【问题标题】:android unable to parse json data androidandroid无法解析json数据android
【发布时间】:2015-08-10 12:12:35
【问题描述】:

我无法解析 JSON 数据,它总是给我 [] 每当我解析时我在 jsonlint.com 上检查了有效的 json,它返回正确的数据

网址是http://egravity.in/csvupload/hello.php/ 在我正在使用的参数中 param.add(new BasicNameValuePair("id", "14")); 因此我希望 url =http://egravity.in/csvupload/hello.php/?id=14

package com.example.prototype.utility;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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.apache.http.impl.conn.DefaultClientConnection;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.StrictMode;
import android.util.Log;
import android.widget.Toast;

@SuppressWarnings({ "deprecation", "unused" })
public class JSONUtil {

    JSONObject result;
    String temp;
    String t;
    InputStream is;
    String exception="";
    static String json = "";
    static JSONObject jObj = null;

  public String setConnection(String url,String method,List <NameValuePair>params){
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        DefaultHttpClient client=new DefaultHttpClient();
        if(method.equalsIgnoreCase("post")){



            HttpResponse httpResponse = null;

            try {

                HttpPost httpPost = new HttpPost(url);
               httpPost.setEntity(new UrlEncodedFormEntity(params));

                t= httpPost.getURI().toString();
    Log.e("ur", t);
                httpResponse = client.execute(httpPost);
                Log.e("response",httpResponse.toString());
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
                Log.e("content_bestfrag ","input stream have "+is.available()  );

        if(is==null)
                Log.e("content ","input stream is null");

        else
            Log.e("content_bestfrag ","input stream is not null "+is.available()  );



            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                exception+=e.getMessage()+"  ";
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                exception+=e.getMessage()+"  ";

            }

        }


        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is));


            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.getMessage());
            }

            Log.e("json", json);
            return json;
    }





}

每当我打印 json(返回的包含数据的字符串)时,它都会显示 [] 而不是数据 请帮帮我……!!!

【问题讨论】:

  • 你确定要发送参数吗?
  • 你能看到你的logcat Log.e("ur", t);有什么价值吗?
  • 是的,我正在发送参数
  • 是的,它没有添加参数,就像 08-10 17:51:27.149: E/ur(21896): egravity.in/csvupload/hello.php
  • 用于解析使用''Gson''库

标签: android json httprequest


【解决方案1】:

您应该发出获取请求而不是发布尝试此代码

    HttpClient client = new DefaultHttpClient();


    URI website = new URI("http://egravity.in/csvupload/hello.php/?id=14"); 
    HttpGet request = new HttpGet();
    request.setURI(website);
    HttpResponse response = client.execute(request);
    response.getStatusLine().getStatusCode();

    in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    StringBuffer sb = new StringBuffer("");
    String l = "";
    String nl = System.getProperty("line.separator");
    while ((l = in.readLine()) != null) {
        sb.append(l + nl);
    }
    in.close();
    data = sb.toString();

【讨论】:

  • 我将如何设置动态传递的参数,例如,我们在帖子中设置
  • 您可以使用 stringbuffer 将参数附加到您的 url
  • 示例:String paramString = URLEncodedUtils.format(params, "utf-8"); StringBuffer stringBuffer = new StringBuffer(URL); stringBuffer .append("?"); stringBuffer .append(paramString);最终 URL 为 String stringBuffer .toString()
【解决方案2】:

您使用 HttpPost,但在像“http://egravity.in/csvupload/hello.php?id=14”这样的 url - HttpGet 参数 id=14

【讨论】:

    【解决方案3】:

    EntityUtils.toString() 很容易得到 HttpResponse 字符串。

    HttpPost post = new HttpPost(url);
    HttpClient client = new DefaultHttpClient();
    
    post.setEntity(new UrlEncodedFormEntity(params1, charset));
    HttpResponse resp = client.execute(post);
    String strResp = EntityUtils.toString(resp.getEntity());
    
    JSONObject jsonResp = new JSONObject(strResp);
    System.out.println(jsonResp.toString(4));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-19
      • 1970-01-01
      • 2019-02-24
      • 1970-01-01
      • 2017-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多