我下面在Android上登錄的優秀教程,並註冊在http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/MySQL的登錄和註冊
我已經實現了代碼爲我自己的項目,但我一直在得到錯誤。所以我決定嘗試一下,如果他的代碼正在工作,它確實沒問題。經過幾個小時的搜尋,我才決定複製他的代碼並從那裏出發。但即使只是將他的代碼複製粘貼到我的項目中,我仍然不斷收到錯誤。
這是我的logcat錯誤,如果任何人都可以指出我在正確的方向我會很高興。
01-15 10:12:26.822: E/AndroidRuntime(1935): FATAL EXCEPTION: main
01-15 10:12:26.822: E/AndroidRuntime(1935): android.os.NetworkOnMainThreadException
01-15 10:12:26.822: E/AndroidRuntime(1935): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
01-15 10:12:26.822: E/AndroidRuntime(1935): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
01-15 10:12:26.822: E/AndroidRuntime(1935): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
01-15 10:12:26.822: E/AndroidRuntime(1935): at libcore.io.IoBridge.connect(IoBridge.java:112)
01-15 10:12:26.822: E/AndroidRuntime(1935): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
01-15 10:12:26.822: E/AndroidRuntime(1935): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
01-15 10:12:26.822: E/AndroidRuntime(1935): at java.net.Socket.connect(Socket.java:842)
01-15 10:12:26.822: E/AndroidRuntime(1935): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
01-15 10:12:26.822: E/AndroidRuntime(1935): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
01-15 10:12:26.822: E/AndroidRuntime(1935): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-15 10:12:26.822: E/AndroidRuntime(1935): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-15 10:12:26.822: E/AndroidRuntime(1935): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-15 10:12:26.822: E/AndroidRuntime(1935): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-15 10:12:26.822: E/AndroidRuntime(1935): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-15 10:12:26.822: E/AndroidRuntime(1935): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-15 10:12:26.822: E/AndroidRuntime(1935): at com.example.androidbasic12.library.JSONParser.getJSONFromUrl(JSONParser.java:42)
01-15 10:12:26.822: E/AndroidRuntime(1935): at com.example.androidbasic12.library.UserFunctions.loginUser(UserFunctions.java:38)
01-15 10:12:26.822: E/AndroidRuntime(1935): at com.example.androidbasic12.MainActivity$1.onClick(MainActivity.java:57)
01-15 10:12:26.822: E/AndroidRuntime(1935): at android.view.View.performClick(View.java:4202)
01-15 10:12:26.822: E/AndroidRuntime(1935): at android.view.View$PerformClick.run(View.java:17340)
01-15 10:12:26.822: E/AndroidRuntime(1935): at android.os.Handler.handleCallback(Handler.java:725)
01-15 10:12:26.822: E/AndroidRuntime(1935): at android.os.Handler.dispatchMessage(Handler.java:92)
01-15 10:12:26.822: E/AndroidRuntime(1935): at android.os.Looper.loop(Looper.java:137)
01-15 10:12:26.822: E/AndroidRuntime(1935): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-15 10:12:26.822: E/AndroidRuntime(1935): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 10:12:26.822: E/AndroidRuntime(1935): at java.lang.reflect.Method.invoke(Method.java:511)
01-15 10:12:26.822: E/AndroidRuntime(1935): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-15 10:12:26.822: E/AndroidRuntime(1935): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-15 10:12:26.822: E/AndroidRuntime(1935): at dalvik.system.NativeStart.main(Native Method)`
JSONParser.class:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
// Making HTTP request
try {
// defaultHttpClient
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();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
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();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
參見[這裏](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) - 我想可能你也有同樣的問題 – GarethL