httpGet无法正常工作,请帮助我

时间:2015-01-30 12:14:31

标签: android

//我在回复时成了问题 我需要你的帮助

try {

        HttpClient client = new DefaultHttpClient();

        HttpGet get = new HttpGet(url);
        HttpResponse response = client.execute(get);
        HttpEntity entity = responseGet.getEntity();
        if (entity != null) {

            responseS = EntityUtils.toString(entity);

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    Log.d("response", responseS);
    return responseS;
}

2 个答案:

答案 0 :(得分:0)

创建一个解析器类,然后在需要的地方创建该类的对象

JSONParser类。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    // function get json from url
    // by making HTTP POST or GET mehtod
    public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if (method == "POST") {
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));
                Log.d("url", url);
                Log.d("params", params.toString());
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            } else if (method == "GET") {
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                Log.d("param", paramString);
                if (!paramString.isEmpty()) {
                    url += "?" + paramString;
                }
                Log.d("url", url);
                HttpGet httpGet = new HttpGet(url);
                Log.d("httpGet", httpGet.toString());
                HttpResponse httpResponse = httpClient.execute(httpGet);
                Log.d("httpResponse", httpResponse.toString());
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
                Log.d("is", is.toString());
            }

        } 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;

    }
}

并像这样调用类的方法。 如果你没有参数,则传递null。

JSONParser jparser = new JSONParser();
JSONObject jobj = jparser.makeHttpRequest("url","GET",null);

答案 1 :(得分:0)

尚未奏效 public static void GETN(String url){         字符串响应;

    try {

        final HttpClient client = new DefaultHttpClient();
        final String uri = url;
        AsyncTask<String, Void, String> task = new AsyncTask<String,Void,String>(){
            @Override
            public String doInBackground(String... params){
                try {
                    HttpGet get = new HttpGet(uri);
                    HttpResponse response = client.execute(get);
                    HttpEntity entity = response.getEntity();
                    String resp = EntityUtils.toString(entity);
                    Log.d("HttpClient","Response: " + resp);
                    return resp;
                } catch(Exception e){
                    e.printStackTrace();
                }

                return null;
            }
        };
        task.execute();
    } catch (Exception e) {
        e.printStackTrace();
    }