我只是面临一个问题,我只是构建了一个演示应用程序,其中通过AsyncTask在列表视图中调用json中的rest api和sho数据是我的代码,并且plz建议我在代码中使用 package test.rest.api;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class showalbooks extends Activity implements OnClickListener {
List<String> list1 = new ArrayList<String>();
String returnString="";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.showalbooks);
}
@Override
public void onClick(View arg0) {
new LongRunningGetIO().execute();
}
private class LongRunningGetIO extends AsyncTask <Void, Void, String> {
protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {
InputStream in = entity.getContent();
StringBuffer out = new StringBuffer();
int n = 1;
while (n>0) {
byte[] b = new byte[4096];
n = in.read(b);
if (n>0) out.append(new String(b, 0, n));
}
return out.toString();
}
@Override
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://192.168.1.156/recess/document/document.json");
HttpClient client = new DefaultHttpClient();
HttpResponse response=null;
try{
response = client.execute(httpGet);}
catch(Exception e){}
System.out.println(response.getStatusLine());
String text = null;
try {
response = httpClient.execute(httpGet, localContext);
HttpEntity entity = response.getEntity();
text = getASCIIContentFromEntity(entity);
} catch (Exception e) {
return e.getLocalizedMessage();
}
String var =text;
try{
JSONObject jObj = new JSONObject(var);
JSONArray jArray = jObj.getJSONArray("document");
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","id: "+json_data.getString("id")+
", title: "+json_data.getString("title")
);
list1.add(json_data.getString("title"));
returnString += "\n" +"id:"+ json_data.getString("id")+" "+"Title:"+ json_data.getString("title");
}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return returnString;
}
//@SuppressWarnings({ "unchecked", "rawtypes" })
protected void onPostExecute(String results) {
if (results!=null) {
setListAdapter(new ArrayAdapter(showalbooks.this, R.layout.showalbooks, list1));
}
}
}
public void setListAdapter(ArrayAdapter<String> arrayAdapter) {
// TODO Auto-generated method stub
}
}
这里是显示错误的日志猫
05-15 18:27:19.582: E/AndroidRuntime(610): FATAL EXCEPTION: main
05-15 18:27:19.582: E/AndroidRuntime(610): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.google.android.gm.contentprovidertest/com.google.android.gm.contentprovidertest.Main}: java.lang.ClassNotFoundException: com.google.android.gm.contentprovidertest.Main
05-15 18:27:19.582: E/AndroidRuntime(610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
05-15 18:27:19.582: E/AndroidRuntime(610): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-15 18:27:19.582: E/AndroidRuntime(610): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-15 18:27:19.582: E/AndroidRuntime(610): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-15 18:27:19.582: E/AndroidRuntime(610): at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 18:27:19.582: E/AndroidRuntime(610): at android.os.Looper.loop(Looper.java:137)
05-15 18:27:19.582: E/AndroidRuntime(610): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-15 18:27:19.582: E/AndroidRuntime(610): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 18:27:19.582: E/AndroidRuntime(610): at java.lang.reflect.Method.invoke(Method.java:511)
05-15 18:27:19.582: E/AndroidRuntime(610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-15 18:27:19.582: E/AndroidRuntime(610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-15 18:27:19.582: E/AndroidRuntime(610): at dalvik.system.NativeStart.main(Native Method)
05-15 18:27:19.582: E/AndroidRuntime(610): Caused by: java.lang.ClassNotFoundException: com.google.android.gm.contentprovidertest.Main
05-15 18:27:19.582: E/AndroidRuntime(610): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
05-15 18:27:19.582: E/AndroidRuntime(610): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-15 18:27:19.582: E/AndroidRuntime(610): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-15 18:27:19.582: E/AndroidRuntime(610): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
05-15 18:27:19.582: E/AndroidRuntime(610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
05-15 18:27:19.582: E/AndroidRuntime(610): ... 11 more
答案 0 :(得分:0)
你必须在清单中写下你的“主要”课程 看到: http://developer.android.com/guide/topics/manifest/manifest-intro.html