我正在尝试创建一个需要用户登录的应用,用户可以在那里注册。我正在使用mySQL。我的桌子还可以。我的代码没有给出任何编译时错误,但是它会产生大量的运行时错误并且只要单击执行HttpPost的创建按钮就强制关闭。不知道该怎么办。我发布了我的代码。请帮助解决代码和错误。
package com.example.learning2;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button crtButton = (Button) findViewById(R.id.button1);
crtButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), newUser.class);
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
// newUser.java
package com.example.learning2;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class newUser extends Activity{
JSONParser jsonParser = new JSONParser();
EditText idForm;
EditText nameForm;
EditText passForm;
final String url_of_post = "http://localhost/Cab_service/create_user.php";
final String TAG_SUCCESS ="success";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sec_view);
idForm = (EditText) findViewById(R.id.userId);
nameForm = (EditText) findViewById(R.id.userName);
passForm = (EditText) findViewById(R.id.userPass);
final Button createButton = (Button) findViewById(R.id.button2);
createButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new createNewUser().execute();
}
});
}
class createNewUser extends AsyncTask<String, String, String>{
protected String doInBackground(String... args){
String pid = idForm.getText().toString();
String user_name = nameForm.getText().toString();
String password = passForm.getText().toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("$pid", pid));
params.add(new BasicNameValuePair("$user_name", user_name));
params.add(new BasicNameValuePair("$password", password));
JSONObject json = jsonParser.makeHttpRequest(url_of_post, "POST", params);
Log.d("Create Response", json.toString());
try{
int success = json.getInt(TAG_SUCCESS);
if(success==1)
{
Toast.makeText(getBaseContext(), "Successful", Toast.LENGTH_SHORT).show();
finish();
}
else
Toast.makeText(getBaseContext(), "UNSuccessful", Toast.LENGTH_SHORT).show();
}catch(JSONException e){ e.printStackTrace();}
return null;
}
}
}
//的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.learning2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.learning2.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".newUser"
android:label="Create New User">
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
// logcat的
12-25 03:48:42.014: W/KeyCharacterMap(432): No keyboard for id 0
12-25 03:48:42.014: W/KeyCharacterMap(432): Using default keymap:
/system/usr/keychars/qwerty.kcm.bin
12-25 03:48:51.835: W/System.err(432): org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused`
12-25 03:48:51.835: W/System.err(432): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:178)
12-25 03:48:51.845: W/System.err(432): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
12-25 03:48:51.845: W/System.err(432): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
12-25 03:48:51.845: W/System.err(432): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
12-25 03:48:51.845: W/System.err(432): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-25 03:48:51.845: W/System.err(432): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-25 03:48:51.855: W/System.err(432): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-25 03:48:51.855: W/System.err(432): at com.example.learning2.JSONParser.makeHttpRequest(JSONParser.java:47)
12-25 03:48:51.855: W/System.err(432): at com.example.learning2.newUser$createNewUser.doInBackground(newUser.java:67)
12-25 03:48:51.855: W/System.err(432): at com.example.learning2.newUser$createNewUser.doInBackground(newUser.java:1)
12-25 03:48:51.855: W/System.err(432): at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-25 03:48:51.855: W/System.err(432): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
12-25 03:48:51.865: W/System.err(432): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-25 03:48:51.865: W/System.err(432): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-25 03:48:51.865: W/System.err(432): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-25 03:48:51.865: W/System.err(432): at java.lang.Thread.run(Thread.java:1019)
12-25 03:48:51.865: W/System.err(432): Caused by: java.net.ConnectException: /127.0.0.1:80 - Connection refused
12-25 03:48:51.875: W/System.err(432): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:207)
12-25 03:48:51.875: W/System.err(432): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)
12-25 03:48:51.875: W/System.err(432): at java.net.Socket.connect(Socket.java:983)
12-25 03:48:51.875: W/System.err(432): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
12-25 03:48:51.885: W/System.err(432): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
12-25 03:48:51.885: W/System.err(432): ... 15 more
12-25 03:48:51.895: E/Buffer Error(432): Error converting result java.lang.NullPointerException
12-25 03:48:51.895: E/JSON Parser(432): Error parsing data org.json.JSONException: End of input at character 0 of
12-25 03:48:51.905: W/dalvikvm(432): threadid=9: thread exiting with uncaught exception (group=0x40015560)
12-25 03:48:51.915: E/AndroidRuntime(432): FATAL EXCEPTION: AsyncTask #1
12-25 03:48:51.915: E/AndroidRuntime(432): java.lang.RuntimeException: An error occured while executing doInBackground()
12-25 03:48:51.915: E/AndroidRuntime(432): at android.os.AsyncTask$3.done(AsyncTask.java:200)
12-25 03:48:51.915: E/AndroidRuntime(432): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
12-25 03:48:51.915: E/AndroidRuntime(432): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
12-25 03:48:51.915: E/AndroidRuntime(432): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
12-25 03:48:51.915: E/AndroidRuntime(432): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-25 03:48:51.915: E/AndroidRuntime(432): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-25 03:48:51.915: E/AndroidRuntime(432): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-25 03:48:51.915: E/AndroidRuntime(432): at java.lang.Thread.run(Thread.java:1019)
12-25 03:48:51.915: E/AndroidRuntime(432): Caused by: java.lang.NullPointerException
12-25 03:48:51.915: E/AndroidRuntime(432): at com.example.learning2.newUser$createNewUser.doInBackground(newUser.java:69)
12-25 03:48:51.915: E/AndroidRuntime(432): at com.example.learning2.newUser$createNewUser.doInBackground(newUser.java:1)
12-25 03:48:51.915: E/AndroidRuntime(432): at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-25 03:48:51.915: E/AndroidRuntime(432): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
12-25 03:48:51.915: E/AndroidRuntime(432): ... 4 more
12-25 03:48:54.045: I/Process(432): Sending signal. PID: 432 SIG: 9
顺便说一下,我是新的android开发人员并没有太多的想法。
谢谢!
我认为问题出在newUser类中。你们可以检查那个班级是否错误。
答案 0 :(得分:0)
解决问题!!
我在应用程序中使用了'localhost'或'127.0.0.1',但是当我在模拟器中运行它时,它将'localhost'作为模拟器localhost而不是我的电脑。将地址更改为PC IP后,连接正常。
但无论如何,谢谢。