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