0

我正在关注关于 Android 登录和注册的优秀教程http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/

我已经将代码实现到我自己的项目中,但我不断收到错误。因此,我决定尝试他的代码是否正常工作并且效果很好。经过几个小时的搜索出了什么问题,我决定复制他的代码并从那里开始。但即使只是将他的代码复制粘贴到我的项目中,我仍然会不断出错。

这是我的 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;

}

}

4

2 回答 2

1

您正在主 UI 线程中执行网络操作。如果您的目标 SDK 是 11 (Honeycomb) 或更高版本,则会抛出 a NetworkOnMainThreadException ,因为此行为可能会阻塞 UI 并导致应用程序无响应。

您必须使用 AsyncTask 通过在其doInBackground(..)方法中加载数据来避免这种情况。

于 2013-01-15T10:27:18.777 回答
0

你得到一个NetworkOnMainThreadException. 这意味着网络操作正在 UI 线程上完成。自 API 11 以来,这在 android 中是不允许的。

将网络操作移至后台线程或改用AsyncTask

本教程可能会有所帮助:

http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html

于 2013-01-15T10:28:20.643 回答