单击注册后,应用程序崩溃

时间:2012-04-23 18:11:05

标签: java android

跟踪:

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文件都存在。

2 个答案:

答案 0 :(得分: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。

答案 1 :(得分:0)

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

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

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