我想获取应用程序的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;
}
}
这是我的json ServiceHandler.java
public class ServiceHandler {
static String response=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();
response = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.e("RESPONSE",response);
return response;
}
}
这是我的错误
org.json.JSONException: Value ���nҩV�͗Qo�0ǿ��ץ`�o���j+� of type java.lang.String cannot be converted to JSONObject
如果我从本地服务器调用文件,如wammp,xampp,那么它正常工作。 请帮我解决这个问题。
答案 0 :(得分:0)
尝试像这样转换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;
}
}