这是错误日志:
03-13 10:00:15.778: W/System.err(2558): org.json.JSONException: No value for objects
03-13 10:00:15.778: W/System.err(2558): at org.json.JSONObject.get(JSONObject.java:354)
03-13 10:00:15.778: W/System.err(2558): at org.json.JSONObject.getJSONArray(JSONObject.java:544)
03-13 10:00:15.778: W/System.err(2558): at com.app.Main$ListTask.doInBackground(Main.java:104)
03-13 10:00:15.778: W/System.err(2558): at com.app.Main$ListTask.doInBackground(Main.java:1)
03-13 10:00:15.778: W/System.err(2558): at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-13 10:00:15.778: W/System.err(2558): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-13 10:00:15.778: W/System.err(2558): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-13 10:00:15.778: W/System.err(2558): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-13 10:00:15.778: W/System.err(2558): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-13 10:00:15.778: W/System.err(2558): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-13 10:00:15.778: W/System.err(2558): at java.lang.Thread.run(Thread.java:856)
活动代码
package com.app;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Main extends Activity {
ListView List;
ArrayList<String> listArray = new ArrayList<String>();
ArrayAdapter<String> Adapter;
Context context;
String url = "http://sandzak.today/api/1/categories/?format=json/api/projekti/10?=";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;
List = (ListView) findViewById(R.id.lista);
Adapter = new ArrayAdapter<String>(this, R.layout.lista, listArray);
List.setAdapter(Adapter);
VideoListTask loaderTask = new VideoListTask();
loaderTask.execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class VideoListTask extends AsyncTask<Void, Void, Void>{
ProgressDialog dialog;
@Override
protected void onPreExecute()
{
dialog = new ProgressDialog(context);
dialog.setTitle("Loading Category");
dialog.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params)
{
HttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(url);
try
{
HttpResponse responce = client.execute(getRequest);
StatusLine statusLine = responce.getStatusLine();
int statusCode = statusLine.getStatusCode();
if(statusCode != 200)
{
return null;
}
InputStream jsonStream = responce.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(jsonStream));
StringBuilder builder = new StringBuilder();
String line;
while((line = reader.readLine())!=null)
{
builder.append(line);
}
String jsonData = builder.toString();
JSONObject json = new JSONObject(jsonData);
JSONObject meta = json.getJSONObject("meta");
JSONArray objects = meta.getJSONArray("objects");
for(int i =0; i<objects.length(); i++)
{
JSONObject video = objects.getJSONObject(i);
listArray.add(video.getString("name"));
}
}
catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
dialog.dismiss();
Adapter.notifyDataSetChanged();
super.onPostExecute(result);
}
}
}
我该如何解决这个问题?
答案 0 :(得分:0)
在此节点上指出要附加字符串,然后将其传递给对象。在对象中,您需要传递字符串,这是json的关键术语。
while((line = reader.readLine())!=null)
{
builder.append(line);
}
答案 1 :(得分:0)
将inputstrem转换为字符串是一种简单的方法:
InputStream inputStream = url.openStream();
Scanner in = new Scanner(inputStream).useDelimiter("\\A");
String str = in.hasNext() ? in.next() : null;
更喜欢使用JSONObject.optXxx(field)
而不是JSONObject.getXxx(field)
,这是你不喜欢的。需要担心json数据中的字段是否可以转换为您想要的类型。此外,如果字段不是JSONObject.optXxx(field, fallback)
,则JSONObject.getXxx()
具有后备值。 t存在或不能转换为特定类型。
(可选)如果您想使用JSONObject.has(fieldname)
,可以先使用{{1}}
希望它&#39;很有用。
答案 2 :(得分:0)
最好在调用服务时使用responsehandler。
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String responseBody = httpclient.execute(httppost, responseHandler);
它会直接给我回复字符串。