我正在尝试通过 HTTP 请求解析一些 JSON 数据。我没有强制关闭我的 AVD(在我的 PC 上),但我确实在我的手机上得到了一个!
这是我的代码:
package com.example.database;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;
public class post extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
HttpResponse response= postData();
HttpEntity entity = response.getEntity();
if(entity != null) {
try {
String responseBody = EntityUtils.toString(entity);
Toast.makeText(getBaseContext(), responseBody, Toast.LENGTH_LONG).show();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public HttpResponse postData() {
HttpResponse response = null;
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("http://192.168.2.4/test.php"));
response = client.execute(request);
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}
}
这是我的清单文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.database"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".FilePickerActivity"
android:label="@string/title_activity_main">
</activity>
<activity android:name=".Dynamic"
android:label="@string/title_activity_main">
</activity>
<activity
android:name=".post"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我手机的 LogCat 条目:
03-24 10:36:16.772: D/dalvikvm(5134): Late-enabling CheckJNI
03-24 10:36:17.282: E/Trace(5134): error opening trace file: No such file or directory (2)
03-24 10:36:17.322: E/dalvikvm(5134): Dex cache directory isn't writable: /data/dalvik-cache
03-24 10:36:17.322: I/dalvikvm(5134): Unable to open or create cache for /data/app/com.example.database-1.apk (/data/dalvik-cache/data@app@com.example.database-1.apk@classes.dex)
03-24 10:36:17.322: D/AndroidRuntime(5134): Shutting down VM
03-24 10:36:17.322: W/dalvikvm(5134): threadid=1: thread exiting with uncaught exception (group=0x41794300)
03-24 10:36:17.362: E/AndroidRuntime(5134): FATAL EXCEPTION: main
03-24 10:36:17.362: E/AndroidRuntime(5134): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.database/com.example.database.post}: java.lang.ClassNotFoundException: com.example.database.post
03-24 10:36:17.362: E/AndroidRuntime(5134): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2105)
03-24 10:36:17.362: E/AndroidRuntime(5134): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2215)
03-24 10:36:17.362: E/AndroidRuntime(5134): at android.app.ActivityThread.access$600(ActivityThread.java:145)
03-24 10:36:17.362: E/AndroidRuntime(5134): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
03-24 10:36:17.362: E/AndroidRuntime(5134): at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 10:36:17.362: E/AndroidRuntime(5134): at android.os.Looper.loop(Looper.java:137)
03-24 10:36:17.362: E/AndroidRuntime(5134): at android.app.ActivityThread.main(ActivityThread.java:4978)
03-24 10:36:17.362: E/AndroidRuntime(5134): at java.lang.reflect.Method.invokeNative(Native Method)
03-24 10:36:17.362: E/AndroidRuntime(5134): at java.lang.reflect.Method.invoke(Method.java:511)
03-24 10:36:17.362: E/AndroidRuntime(5134): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-24 10:36:17.362: E/AndroidRuntime(5134): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
03-24 10:36:17.362: E/AndroidRuntime(5134): at dalvik.system.NativeStart.main(Native Method)
03-24 10:36:17.362: E/AndroidRuntime(5134): Caused by: java.lang.ClassNotFoundException: com.example.database.post
03-24 10:36:17.362: E/AndroidRuntime(5134): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
03-24 10:36:17.362: E/AndroidRuntime(5134): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
03-24 10:36:17.362: E/AndroidRuntime(5134): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
03-24 10:36:17.362: E/AndroidRuntime(5134): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
03-24 10:36:17.362: E/AndroidRuntime(5134): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2096)
03-24 10:36:17.362: E/AndroidRuntime(5134): ... 11 more
我怎么能解决这个问题?非常感谢!