我的方法如何返回一个String

时间:2015-05-30 16:07:01

标签: java android

我正在编写Android应用程序并遇到障碍。

我无法获得某种返回字符串的方法。

我正在使用 loopjs Async Http Client

代码如下

public static String networkOps(final String relativeURL){
AsyncHttpClient client = new AsyncHttpClient();
client.get(BASE_URL+relativeURL, new TextHttpResponseHandler() {

        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            Log.e(TAG,responseString);
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String responseString) {
            String response = responseString;
            Log.i(TAG,responseString);
            Log.i(TAG,BASE_URL+relativeURL);
        }
    });
    return response;
}

2 个答案:

答案 0 :(得分:0)

尝试:

public static String networkOps(final String relativeURL){
String response = null;
AsyncHttpClient client = new AsyncHttpClient();
client.get(BASE_URL+relativeURL, new TextHttpResponseHandler() {

    @Override
    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

        Log.e(TAG,responseString);
    }

    @Override
    public void onSuccess(int statusCode, Header[] headers, String responseString) {
        response = responseString;
        Log.i(TAG,responseString);
        Log.i(TAG,BASE_URL+relativeURL);
    }
});
return response;

}

<强>更新

或者你可以按方法获得响应字符串:

public String makeServiceCall(String url, int method,
            List<NameValuePair> params) {
        try {
            // http client
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;

            // Checking http request method type
            if (method == POST) {
                HttpPost httpPost = new HttpPost(url);
                // adding post params
                if (params != null) {
                    httpPost.setEntity(new UrlEncodedFormEntity(params));
                }

                httpResponse = httpClient.execute(httpPost);

            } else if (method == GET) {
                // appending params to url
                if (params != null) {
                    String paramString = URLEncodedUtils
                            .format(params, "utf-8");
                    url += "?" + paramString;
                }
                HttpGet httpGet = new HttpGet(url);

                httpResponse = httpClient.execute(httpGet);

            }
            httpEntity = httpResponse.getEntity();
            response = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return response;

    }

答案 1 :(得分:0)

传递ResponseHandler并实现回调。

在您的活动中:

private void doNetworkCall() {
    NetworkUtil.networkOps(final String relativeURL, new TextHttpResponseHandler() {
    @Override
    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

        Log.e(TAG,responseString);
    }

    @Override
    public void onSuccess(int statusCode, Header[] headers, String responseString) {
        doSomethingWithResponse(responseString);
        Log.i(TAG,BASE_URL+relativeURL);
    }
 });

 private void doSomethingWithResponse(String response) {
      ....
      Log.i(TAG,responseString);
 }