简体   繁体   English

如何使用Http方法在android中回应服务器响应

[英]How to echo the server response in android with Http method

I am calling the JSONParser class with the 我正在使用JSONParser类

List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(urlServer, "GET", params);

The JSONParser is this JSONParser是这个

public class JSONParser {
    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public JSONObject makeHttpRequest(String url, String method,
                                      List<NameValuePair> params) {

        // Making HTTP request
        try {

            if (method == "GET") {

                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
                Log.e("Pasa x aki","ssss");
            }

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

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object

        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;
    }
}

My error is in "JSON Parser" because the json object is null . 我的错误是在“ JSON分析器”中,因为json objectnull I would like to know what is happening in the server . 我想知道server正在发生什么。 How can I print the echo from the server ? 如何打印来自server的回显?

Try this 尝试这个

HttpEntity entity = response.getEntity();

        if (entity != null) {
           String retSrc = EntityUtils.toString(entity); 
          Log.v("response server ", retSrc);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android中获取HTTP响应的替代方法 - alternative method for getting http response in android Android上的Http Post:服务器请求和响应 - Http Post on Android: Server Request and Response 如何通过Http post方法使用android将数据发送到服务器? - how to send data to server using android by Http post method? 响应主体中印有Android手机中服务器的HTTP响应状态代码 - Status code of HTTP response of server in Android phone is printed in the body of response Android Echo Server错误 - Android Echo Server Error 如何在Android / Java中使用Google Volley for HTTP GET请求将响应头字段返回给main方法? - How to return response header field to main method using Google Volley for HTTP GET request in Android / Java? Android设备是否发出始终相同的HTTP请求或服务器始终给出相同的响应? 那怎么可能? - Android device is making always the same HTTP request or server giving always the same response? How is that possible? 如何创建一个虚拟REST服务器API以检查我的android http request \\ response? - how to create a dummy REST server API to check my android http request\response? 如何在android上实现一个HTTP服务器 - How to implement an HTTP server on android 如何在Android中将HTTP响应放入数组 - How to put http response into array in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM