如何在Android中获得JSON响应?

如何在Android中获得JSON响应?,android,android-json,Android,Android Json,我想为我的应用程序获取json数据 下面是获得响应的CallAPI.java类 public class CallAPI extends AsyncTask<String,String,String> { String jsonStr=""; private static final String url="https://api.litzscore.com/rest/v2/recent_matches/"; @Override protected

我想为我的应用程序获取json数据

下面是获得响应的
CallAPI.java

public class CallAPI extends AsyncTask<String,String,String> {

    String jsonStr="";
    private static final String url="https://api.litzscore.com/rest/v2/recent_matches/";

    @Override
    protected String doInBackground(String... params) {
        List<NameValuePair> param=new ArrayList<>();
        param.add(new BasicNameValuePair("access_token",""));//here i am added my access token
        ServiceHandler sh=new ServiceHandler();
        jsonStr=sh.makeServiceCall(url,ServiceHandler.GET,param);

        try{
            JSONObject obj=new JSONObject(jsonStr);//I got error here

            Log.e("response",""+obj.toString());
        }
        catch (JSONException e){
            e.printStackTrace();
        }
        return null;
    }
}
这是我的错误

org.json.JSONException: Value ���nҩV�͗Qo�0ǿ��ץ`�o���j+� of type java.lang.String cannot be converted to JSONObject

如果我从本地服务器(如wammp、xampp)调用该文件,那么它工作正常。

尝试像这样转换json响应

public class ServiceHandler {
static String response=null;
InputStream is = null;
public final static int GET = 1;
public final static int POST = 2;

public ServiceHandler() {

}

public String makeServiceCall(String url, int method) {
    return this.makeServiceCall(url, method, null);
}
public String makeServiceCall(String url, int method,
                              List<NameValuePair> params) {
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);
            if (params != null) {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
            }

            httpResponse = httpClient.execute(httpPost);

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

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

        //convert response to string
        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();
        response = sb.toString();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Log.e("RESPONSE",response);
    return response;

}

}
公共类ServiceHandler{
静态字符串响应=null;
InputStream=null;
公共最终静态int GET=1;
公共最终静态int POST=2;
公共服务处理程序(){
}
公共字符串makeServiceCall(字符串url,int方法){
返回此.makeServiceCall(url,方法,null);
}
公共字符串makeServiceCall(字符串url,int方法,
列表参数){
试一试{
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpEntity HttpEntity=null;
HttpResponse HttpResponse=null;
if(方法==POST){
HttpPost HttpPost=新的HttpPost(url);
如果(参数!=null){
setEntity(新的UrlEncodedFormEntity(参数));
}
httpResponse=httpClient.execute(httpPost);
}else if(方法==GET){
如果(参数!=null){
String paramString=URLEncodedUtils
.格式(参数“utf-8”);
url+=“?”+参数字符串;
}
Log.e(“URL”,“URL+URL”);
HttpGet HttpGet=新的HttpGet(url);
httpResponse=httpClient.execute(httpGet);
}
httpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
//将响应转换为字符串
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
response=sb.toString();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
Log.e(“响应”,响应);
返回响应;
}
}

���nҩV�͗Qo�0ǿ��ץ
�o���j+�` 无效的JSON字符串这就是问题所在,我该怎么办?在
Log.e(“RESPONSE”,RESPONSE)中从服务器获取什么信息行?在响应日志中记录���nҩV�͗Qo�0ǿ��ץ`�o��......... 就像你们在浏览器上检查api的响应或者其他什么,它会重新返回相同的
���nҩV�͗Qo�0ǿ��ץ`�o���j+�?不,它不起作用,因为我的服务提供商提供了gzip格式的数据,所以我需要解压缩这些数据。这不是你的错。@MilanGajera我认为你应该为你的课程添加更多细节。例如:关于服务器的响应格式。这很容易,因为每个人都能帮助你。我得到了这样的回应���nҩV�͗Qo�0ǿ��ץ`�o���j+�我的意思是,编辑您的问题,并添加关于为什么您的服务器返回gzip格式而不是json格式的信息。显然,找到解决这个问题的办法是必要的。
public class ServiceHandler {
static String response=null;
InputStream is = null;
public final static int GET = 1;
public final static int POST = 2;

public ServiceHandler() {

}

public String makeServiceCall(String url, int method) {
    return this.makeServiceCall(url, method, null);
}
public String makeServiceCall(String url, int method,
                              List<NameValuePair> params) {
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);
            if (params != null) {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
            }

            httpResponse = httpClient.execute(httpPost);

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

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

        //convert response to string
        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();
        response = sb.toString();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Log.e("RESPONSE",response);
    return response;

}

}