私は Android と Java の初心者で、PHP と Web プログラミングの経験は十分にあります。Android からサーバー (Localhost) に POST データを送信し、サーバーに表示したいと考えています。
[更新ログを編集]
これが私のコードです:
public class NewUserActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_user);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_new_user, menu);
return true;
}
public void addNewUser(View view) {
EditText em = (EditText) findViewById(R.id.email);
EditText na = (EditText) findViewById(R.id.name);
EditText pa = (EditText) findViewById(R.id.pass);
String e = em.getText().toString();
String n = na.getText().toString();
String p = pa.getText().toString();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/mapptor/register.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("email", e));
nameValuePairs.add(new BasicNameValuePair("name", n));
nameValuePairs.add(new BasicNameValuePair("pass",p));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
@SuppressWarnings("unused")
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException exc) {
Log.d("T1", "HERE1");
} catch (IOException exc) {
Log.d("T2", "HERE2");
}
}
問題は、これを実行すると (エミュレーターで) FORCE CLOSED が発生し続けることです。
だから私はちょっとログを調べました:
12-30 13:36:54.940: I/ActivityManager(62): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.mapptor.android/.NewUserActivity } from pid 130
12-30 13:36:54.980: I/ActivityManager(62): Start proc com.mapptor.android for activity com.mapptor.android/.NewUserActivity: pid=616 uid=10036 gids={}
12-30 13:36:56.110: I/ActivityManager(62): Displayed com.mapptor.android/.NewUserActivity: +1s133ms
12-30 13:37:02.660: W/KeyCharacterMap(116): No keyboard for id 0
12-30 13:37:02.670: W/KeyCharacterMap(116): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
12-30 13:37:04.240: W/KeyCharacterMap(616): No keyboard for id 0
12-30 13:37:04.240: W/KeyCharacterMap(616): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
12-30 13:37:04.640: D/dalvikvm(116): GC_EXTERNAL_ALLOC freed 254K, 58% free 3570K/8327K, external 7069K/7413K, paused 212ms
12-30 13:37:04.761: W/KeyCharacterMap(116): No keyboard for id 0
12-30 13:37:04.761: W/KeyCharacterMap(116): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
12-30 13:37:08.690: D/AndroidRuntime(616): Shutting down VM
12-30 13:37:08.690: W/dalvikvm(616): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-30 13:37:08.710: E/AndroidRuntime(616): FATAL EXCEPTION: main
12-30 13:37:08.710: E/AndroidRuntime(616): java.lang.IllegalStateException: Could not execute method of the activity
12-30 13:37:08.710: E/AndroidRuntime(616): at android.view.View$1.onClick(View.java:2144)
12-30 13:37:08.710: E/AndroidRuntime(616): at android.view.View.performClick(View.java:2485)
12-30 13:37:08.710: E/AndroidRuntime(616): at android.view.View$PerformClick.run(View.java:9080)
12-30 13:37:08.710: E/AndroidRuntime(616): at android.os.Handler.handleCallback(Handler.java:587)
12-30 13:37:08.710: E/AndroidRuntime(616): at android.os.Handler.dispatchMessage(Handler.java:92)
12-30 13:37:08.710: E/AndroidRuntime(616): at android.os.Looper.loop(Looper.java:123)
12-30 13:37:08.710: E/AndroidRuntime(616): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-30 13:37:08.710: E/AndroidRuntime(616): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 13:37:08.710: E/AndroidRuntime(616): at java.lang.reflect.Method.invoke(Method.java:507)
12-30 13:37:08.710: E/AndroidRuntime(616): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-30 13:37:08.710: E/AndroidRuntime(616): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-30 13:37:08.710: E/AndroidRuntime(616): at dalvik.system.NativeStart.main(Native Method)
12-30 13:37:08.710: E/AndroidRuntime(616): Caused by: java.lang.reflect.InvocationTargetException
12-30 13:37:08.710: E/AndroidRuntime(616): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 13:37:08.710: E/AndroidRuntime(616): at java.lang.reflect.Method.invoke(Method.java:507)
12-30 13:37:08.710: E/AndroidRuntime(616): at android.view.View$1.onClick(View.java:2139)
12-30 13:37:08.710: E/AndroidRuntime(616): ... 11 more
12-30 13:37:08.710: E/AndroidRuntime(616): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters.
12-30 13:37:08.710: E/AndroidRuntime(616): at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:572)
12-30 13:37:08.710: E/AndroidRuntime(616): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:292)
12-30 13:37:08.710: E/AndroidRuntime(616): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-30 13:37:08.710: E/AndroidRuntime(616): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-30 13:37:08.710: E/AndroidRuntime(616): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-30 13:37:08.710: E/AndroidRuntime(616): at com.mapptor.android.NewUserActivity.addNewUser(NewUserActivity.java:61)
12-30 13:37:08.710: E/AndroidRuntime(616): ... 14 more
12-30 13:37:08.730: W/ActivityManager(62): Force finishing activity com.mapptor.android/.NewUserActivity
これをデバッグできません。問題は何ですか?
ありがとうございました。:)