gpt4 book ai didi

android - android中的Json token 错误

转载 作者:行者123 更新时间:2023-11-30 03:08:52 25 4
gpt4 key购买 nike

我试图在没有网络连接时运行我的项目。如果网络可用,则不会出现问题。

我遇到了这个错误,

01-24 17:41:19.725: E/AndroidRuntime(1077): FATAL EXCEPTION: AsyncTask #3
01-24 17:41:19.725: E/AndroidRuntime(1077): java.lang.RuntimeException: An error occured while executing doInBackground()
01-24 17:41:19.725: E/AndroidRuntime(1077): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-24 17:41:19.725: E/AndroidRuntime(1077): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
01-24 17:41:19.725: E/AndroidRuntime(1077): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
01-24 17:41:19.725: E/AndroidRuntime(1077): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
01-24 17:41:19.725: E/AndroidRuntime(1077): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-24 17:41:19.725: E/AndroidRuntime(1077): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-24 17:41:19.725: E/AndroidRuntime(1077): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-24 17:41:19.725: E/AndroidRuntime(1077): at java.lang.Thread.run(Thread.java:856)
01-24 17:41:19.725: E/AndroidRuntime(1077): Caused by: java.lang.NullPointerException
01-24 17:41:19.725: E/AndroidRuntime(1077): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
01-24 17:41:19.725: E/AndroidRuntime(1077): at org.json.JSONTokener.nextValue(JSONTokener.java:94)
01-24 17:41:19.725: E/AndroidRuntime(1077): at org.json.JSONObject.<init>(JSONObject.java:154)
01-24 17:41:19.725: E/AndroidRuntime(1077): at org.json.JSONObject.<init>(JSONObject.java:171)
01-24 17:41:19.725: E/AndroidRuntime(1077): at com.example.sms.JsonParser.makeHttpRequest(JsonParser.java:136)
01-24 17:41:19.725: E/AndroidRuntime(1077): at co m.example.sms.LoginActivity$AttemptLogin.doInBackground(LoginActivity.java:298)
01-24 17:41:19.725: E/AndroidRuntime(1077): at com.example.sms.LoginActivity$AttemptLogin.doInBackground(LoginActivity.java:1)
01-24 17:41:19.725: E/AndroidRuntime(1077): at

这是我的 JsonParser 类,

  public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {


try {

json=null;
jsonObject= null;
if(method.equalsIgnoreCase("POST")){


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();

System.out.println(is);

System.out.println("getting the content");

}else if(method.equalsIgnoreCase("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();
}


System.out.println("getting all the values ");

try {
json = null;
jsonObject =null;
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 {

Log.i("tagconvertstr1", "["+json+"]");
//if(TextUtils.isEmpty(json))
if(json!=null||json!="null")
{
jss= "not empty";
System.out.println("json is not equal to null");
jsonObject = new JSONObject(json);
System.out.println("json object parse finished");

Log.i("tagconvertstr2","["+json+"]");

}

else
{
System.out.println(" it is null value ");
jss= "empty";
System.out.println("jss value is" +jss);

}


} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}


System.out.println("jsson object is" + json);

return jsonObject;

}

我不知道为什么即使我检查了 null 也会出现此错误!任何人都可以帮助我吗?

最佳答案

正如您所说,网络断开时会发生此错误。这意味着您收到 NULL 值并将 NULL 值传递给 json。如果将 null 分配给构造函数,则 JSONObject 无法捕获错误。因此,要解决此问题,请替换以下代码

jsonObject=new JSONObject(json) ;

jsonObject=new JSONObject(json == null ? "" : json) ;

关于android - android中的Json token 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21324602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com