-1

is = httpEntity.getContent();からへ のカーソル ジャンプをデバッグするcatch (IOException e) {e.printStackTrace();}と、データベースにエントリがありません。

 function get json from url
by making HTTP POST or GET method
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));

            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");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }           

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

2 に答える 2

0

期待される結果が JSON の場合、次のように応答オブジェクトから簡単に取得できます。

HttpResponse httpResponse = httpClient.execute(httpGet);
String responseBody = EntityUtils.toString(httpResponse.getEntity());
JSONObject responseObject = new JSONObject(responseBody);

それが役立つことを願っています。

于 2013-10-26T05:23:28.223 に答える