このチュートリアルに従って、HttpClientを使用してAndroidログイン画面のエラーが発生しました
ログインできないのはなぜですか? 私のターゲット バージョンは targetSdkVersion 15 と minSdkVersion 12 です。これは、ボタンを押してログインするときのエラーです。
主な活動
public class MainActivity extends Activity implements OnClickListener {
Button ok, back, exit;
TextView result;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ok = (Button) findViewById(R.id.btn_login);
ok.setOnClickListener(this);
result = (TextView) findViewById(R.id.lbl_result);
}
public void postLoginData() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.sencide.com/blog/login.php");
try {
EditText uname = (EditText) findViewById(R.id.txt_username);
String username = uname.getText().toString();
EditText pword = (EditText) findViewById(R.id.txt_password);
String password = pword.getText().toString();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.w("SENCIDE", "Execute HTTP Post Request");
HttpResponse response = httpclient.execute(httppost);
String str = inputStreamToString(response.getEntity().getContent()).toString();
Log.w("SENCIDE", str);
if (str.toString().equalsIgnoreCase("true")) {
Log.w("SENCIDE", "TRUE");
result.setText("Login successful");
} else {
Log.w("SENCIDE", "FALSE");
result.setText(str);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return total;
}
public void onClick(View view) {
if (view == ok) {
postLoginData();
}
}
}
エラー
09-18 10:50:40.531: E/AndroidRuntime(23081): FATAL EXCEPTION: main
09-18 10:50:40.531: E/AndroidRuntime(23081): android.os.NetworkOnMainThreadException
09-18 10:50:40.531: E/AndroidRuntime(23081): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
09-18 10:50:40.531: E/AndroidRuntime(23081): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
09-18 10:50:40.531: E/AndroidRuntime(23081): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
09-18 10:50:40.531: E/AndroidRuntime(23081): at java.net.InetAddress.getAllByName(InetAddress.java:220)
09-18 10:50:40.531: E/AndroidRuntime(23081): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
09-18 10:50:40.531: E/AndroidRuntime(23081): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-18 10:50:40.531: E/AndroidRuntime(23081): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-18 10:50:40.531: E/AndroidRuntime(23081): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
09-18 10:50:40.531: E/AndroidRuntime(23081): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-18 10:50:40.531: E/AndroidRuntime(23081): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-18 10:50:40.531: E/AndroidRuntime(23081): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
09-18 10:50:40.531: E/AndroidRuntime(23081): at com.example.login.MainActivity.postLoginData(MainActivity.java:63)
09-18 10:50:40.531: E/AndroidRuntime(23081): at com.example.login.MainActivity.onClick(MainActivity.java:101)
09-18 10:50:40.531: E/AndroidRuntime(23081): at android.view.View.performClick(View.java:3558)
09-18 10:50:40.531: E/AndroidRuntime(23081): at android.view.View$PerformClick.run(View.java:14157)
09-18 10:50:40.531: E/AndroidRuntime(23081): at android.os.Handler.handleCallback(Handler.java:605)
09-18 10:50:40.531: E/AndroidRuntime(23081): at android.os.Handler.dispatchMessage(Handler.java:92)
09-18 10:50:40.531: E/AndroidRuntime(23081): at android.os.Looper.loop(Looper.java:137)
09-18 10:50:40.531: E/AndroidRuntime(23081): at android.app.ActivityThread.main(ActivityThread.java:4514)
09-18 10:50:40.531: E/AndroidRuntime(23081): at java.lang.reflect.Method.invokeNative(Native Method)
09-18 10:50:40.531: E/AndroidRuntime(23081): at java.lang.reflect.Method.invoke(Method.java:511)
09-18 10:50:40.531: E/AndroidRuntime(23081): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
09-18 10:50:40.531: E/AndroidRuntime(23081): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
09-18 10:50:40.531: E/AndroidRuntime(23081): at dalvik.system.NativeStart.main(Native Method)