0

痕迹:

04-23 18:21:28.380: D/dalvikvm(628): GC_FOR_ALLOC freed 48K, 4% free 6396K/6595K, paused 76ms
04-23 18:21:28.390: I/dalvikvm-heap(628): Grow heap (frag case) to 6.797MB for 513744-byte allocation
04-23 18:21:28.520: D/dalvikvm(628): GC_FOR_ALLOC freed 3K, 4% free 6894K/7111K, paused 81ms
04-23 18:21:28.660: D/dalvikvm(628): GC_CONCURRENT freed <1K, 4% free 6894K/7111K, paused 4ms+13ms
04-23 18:22:09.350: D/AndroidRuntime(628): Shutting down VM
04-23 18:22:09.350: W/dalvikvm(628): threadid=1: thread exiting with uncaught exception (group=0x40014760)
04-23 18:22:09.370: E/AndroidRuntime(628): FATAL EXCEPTION: main
04-23 18:22:09.370: E/AndroidRuntime(628): android.os.NetworkOnMainThreadException
04-23 18:22:09.370: E/AndroidRuntime(628):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
04-23 18:22:09.370: E/AndroidRuntime(628):  at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:368)
04-23 18:22:09.370: E/AndroidRuntime(628):  at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:208)
04-23 18:22:09.370: E/AndroidRuntime(628):  at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:431)
04-23 18:22:09.370: E/AndroidRuntime(628):  at java.net.Socket.connect(Socket.java:901)
04-23 18:22:09.370: E/AndroidRuntime(628):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
04-23 18:22:09.370: E/AndroidRuntime(628):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
04-23 18:22:09.370: E/AndroidRuntime(628):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-23 18:22:09.370: E/AndroidRuntime(628):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-23 18:22:09.370: E/AndroidRuntime(628):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-23 18:22:09.370: E/AndroidRuntime(628):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-23 18:22:09.370: E/AndroidRuntime(628):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-23 18:22:09.370: E/AndroidRuntime(628):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-23 18:22:09.370: E/AndroidRuntime(628):  at library.JSONParser.getJSONFromUrl(JSONParser.java:42)
04-23 18:22:09.370: E/AndroidRuntime(628):  at library.UserFunctions.registerUser(UserFunctions.java:61)
04-23 18:22:09.370: E/AndroidRuntime(628):  at com.example.AndroidLoginAndRegistration.RegisterActivity$1.onClick(RegisterActivity.java:55)
04-23 18:22:09.370: E/AndroidRuntime(628):  at android.view.View.performClick(View.java:3110)
04-23 18:22:09.370: E/AndroidRuntime(628):  at android.view.View$PerformClick.run(View.java:11934)
04-23 18:22:09.370: E/AndroidRuntime(628):  at android.os.Handler.handleCallback(Handler.java:587)
04-23 18:22:09.370: E/AndroidRuntime(628):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-23 18:22:09.370: E/AndroidRuntime(628):  at android.os.Looper.loop(Looper.java:132)
04-23 18:22:09.370: E/AndroidRuntime(628):  at android.app.ActivityThread.main(ActivityThread.java:4123)
04-23 18:22:09.370: E/AndroidRuntime(628):  at java.lang.reflect.Method.invokeNative(Native Method)
04-23 18:22:09.370: E/AndroidRuntime(628):  at java.lang.reflect.Method.invoke(Method.java:491)
04-23 18:22:09.370: E/AndroidRuntime(628):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
04-23 18:22:09.370: E/AndroidRuntime(628):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
04-23 18:22:09.370: E/AndroidRuntime(628):  at dalvik.system.NativeStart.main(Native Method)
04-23 18:22:12.530: I/Process(628): Sending signal. PID: 628 SIG: 9

代码:

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;

public class UserFunctions {

private JSONParser jsonParser;

// Testing in localhost using wamp or xampp
// use http://10.0.2.2/ to connect to your localhost ie http://localhost/
private static String loginURL = "http://10.0.2.2/android_login_api/";
private static String registerURL = "http://10.0.2.2/android_login_api/";

private static String login_tag = "login";
private static String register_tag = "register";

// constructor
public UserFunctions(){
    jsonParser = new JSONParser();
}

/**
 * function make Login Request
 * @param email
 * @param password
 * */
public JSONObject loginUser(String email, String password){
    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", login_tag));
    params.add(new BasicNameValuePair("email", email));
    params.add(new BasicNameValuePair("password", password));
    JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
    // return json
    // Log.e("JSON", json.toString());
    return json;
}

/**
 * function make Login Request
 * @param name
 * @param email
 * @param password
 * */
public JSONObject registerUser(String name, String email, String password){
    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", register_tag));
    params.add(new BasicNameValuePair("name", name));
    params.add(new BasicNameValuePair("email", email));
    params.add(new BasicNameValuePair("password", password));

    // getting JSON Object
    JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
    // return json
    return json;
}

/**
 * Function get Login status
 * */
public boolean isUserLoggedIn(Context context){
    DatabaseHandler db = new DatabaseHandler(context);
    int count = db.getRowCount();
    if(count > 0){
        // user logged in
        return true;
    }
    return false;
}

/**
 * Function to logout user
 * Reset Database
 * */
public boolean logoutUser(Context context){
    DatabaseHandler db = new DatabaseHandler(context);
    db.resetTables();
    return true;
}

logcat 输出可以在顶部找到。下面是我的用户功能的代码,尽管我尽了最大努力,但它仍然不起作用。我曾尝试将 URL 更改为 localhost 等,但它没有将数据保存到 xampp,而是崩溃了。我在库位中有 JSONParse 和 DataBaseHandler,但我不知道问题出在哪里。甚至登录页面也只是崩溃而不是显示错误消息。我已确保所有必需的 php 文件都存在。

4

2 回答 2

0

NetworkOnMainThreadException

这仅针对面向 Honeycomb SDK 或更高版本的应用程序抛出。允许针对早期 SDK 版本的应用程序在其主事件循环线程上进行网络连接,但非常不鼓励这样做

Android 添加了一个DownloadFilesTask。您需要使用 DownloadFilesTask。

 private class LoginTask extends AsyncTask<String, Integer, Long> {
 private private static String loginURL = "http://10.0.2.2/android_login_api/"; //remove other one
 protected JSONObject doInBackground(String... params) {
     List<NameValuePair> params = new ArrayList<NameValuePair>();
     for(int i=0; i<params.length; i+=2){ //increment i by length of pair(2)
         params.add(new BasicNameValuePair(params[i], params[i+1])); 
     }
     return jsonParser.getJSONFromUrl(loginURL, params);
 }

 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(Long result) {
     showDialog("Downloaded " + result + " bytes");
 }

修改您的登录功能。由此

public JSONObject loginUser(String email, String password){
    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", login_tag));
    params.add(new BasicNameValuePair("email", email));
    params.add(new BasicNameValuePair("password", password));
    JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
    // return json
    // Log.e("JSON", json.toString());
    return json;
}

对此

public JSONObject loginUser(String email, String password){
    // Building Parameters
    String[] params = {"tag", login_tag, "email", email, "password", password};
    JSONObject json = new LoginTask().execute(params);
    return json;
}

这是一个小技巧,你时间紧迫。您必须为每个功能做类似的事情,并完成它自己的任务。如果字符串数组中的第一个参数是您想要执行的函数,则可以使其更加模块化。然后让下载任务使用一些分支来确定要传递给函数 getJSONFromUrl 的 URL。

于 2012-04-23T18:44:13.767 回答
0

您正在主线程上运行网络操作。至少在 ICS 中这是不允许的,你会得到那个例外。我在为 Android 2.3.3 开发应用程序时遇到了同样的问题,它很好,但在 4.0.3 上崩溃了。

我无法为您提供修改后的代码,因为您的方法必须进行一些重大更改。您必须在另一个线程上运行登录和注册功能,并且不知道如何调用它们。在它们中的每一个中创建一个线程很容易,但是从方法返回结果是不可能的。

正确的实现是在不同的线程上运行它们,并在获得结果时通知主线程。发送通知的一种方法是使用传递给新创建线程的处理程序。我在这里找到了一个例子:http ://www.helloandroid.com/tutorials/progressbar-updating-using-message-handler

于 2012-04-23T19:34:31.863 回答