好吧我知道它被问了很多,但我找不到明显的错误答案,所以请你们帮忙。
当我没有互联网连接时,我收到此错误:
java.lang.NullPointerException
at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
org.json.JSONTokener.nextValue(JSONTokener.java:94)
这里是logcat指向的地方:
JSONObject jsonObj = new JSONObject(myJSON);
完整代码
public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://justedhak.comlu.com/get-data.php");
// Depends on your web service
// httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
File httpCacheDir = new File(getApplicationContext().getExternalCacheDir(),"http");
long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
HttpResponseCache.install(httpCacheDir, httpCacheSize);
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
@Override
protected void onPostExecute(String result){
myJSON=result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
protected void showList(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray("result");
String[] vname = new String[peoples.length()];
String[] vpath =new String[peoples.length()];
for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
vname[i] = c.getString("name");
vpath[i]=c.getString("path");
我正在使用picasso
库并且图像被缓存,所以即使我没有互联网我想要打开应用程序并看到缓存的图像怎么做?
编辑php
$con=mysqli_connect($host,$uname,$pwd,$db);
$sql = "select id, name, path from image";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array('id'=>$row[0],
'name'=>$row[1],
'path'=>$row[2]
));
}
echo json_encode(array("result"=>$result));
mysql_close($con);
?>