我必須登錄到在ASP做了一個網站一個問題... 這裏是我使用的代碼:Java的Android的HTTP POST登錄錯誤
public class MainActivity extends Activity {
Button bottone;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottone = (Button) findViewById(R.id.button1);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy =
new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
bottone.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// starting new Async Task
try {
login();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public static void login() throws IOException
{
String loginurl = "https://www.blurum.it/Web/", user, pass;
user = "tester55";
pass = "provapassword321";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(loginurl);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("ctrl_LoginControl$loginView$c01$RPLogin$LoginView$Username", user));
nameValuePairs.add(new BasicNameValuePair("ctrl_LoginControl$loginView$c01$RPLogin$LoginView$Password", pass));
nameValuePairs.add(new BasicNameValuePair("remember", "on"));
nameValuePairs.add(new BasicNameValuePair("ctrl_LoginControl$loginView$c01$RPLogin$LoginView$LoginButton", "Accedi"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpClient.execute(httpPost);
Log.d("logggd", "response stat code " + response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() < 400) {
String cookie = response.getFirstHeader("Cookie")
.getValue();
Log.d("logggdc", "cookie: " + cookie);
// get the contacts page
HttpGet getContacts = new HttpGet("https://www.blurum.it/Web/default.aspx");
getContacts.setHeader("Cookie", cookie);
response = httpClient.execute(getContacts);
InputStream ins = response.getEntity().getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(
ins));
String inputLine;
while ((inputLine = in.readLine()) != null) {
Log.d("logggd1", " " + inputLine);
}
in.close();
} else {
Log.d("logggd2", "Response error: "
+ response.getStatusLine().getStatusCode());
}
}
}
但我的應用程序崩潰,所以我不能在代碼中找出錯誤...這裏是logcat的:
11-12 20:05:37.678: W/dalvikvm(1806): threadid=1: thread exiting with uncaught exception (group=0xb4177180)
11-12 20:05:37.688: E/AndroidRuntime(1806): FATAL EXCEPTION: main
11-12 20:05:37.688: E/AndroidRuntime(1806): java.lang.NullPointerException
11-12 20:05:37.688: E/AndroidRuntime(1806): at com.example.testlogin.MainActivity.login(MainActivity.java:93)
11-12 20:05:37.688: E/AndroidRuntime(1806): at com.example.testlogin.MainActivity$1.onClick(MainActivity.java:53)
11-12 20:05:37.688: E/AndroidRuntime(1806): at android.view.View.performClick(View.java:3511)
11-12 20:05:37.688: E/AndroidRuntime(1806): at android.view.View$PerformClick.run(View.java:14105)
11-12 20:05:37.688: E/AndroidRuntime(1806): at android.os.Handler.handleCallback(Handler.java:605)
11-12 20:05:37.688: E/AndroidRuntime(1806): at android.os.Handler.dispatchMessage(Handler.java:92)
11-12 20:05:37.688: E/AndroidRuntime(1806): at android.os.Looper.loop(Looper.java:137)
11-12 20:05:37.688: E/AndroidRuntime(1806): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-12 20:05:37.688: E/AndroidRuntime(1806): at java.lang.reflect.Method.invokeNative(Native Method)
11-12 20:05:37.688: E/AndroidRuntime(1806): at java.lang.reflect.Method.invoke(Method.java:511)
11-12 20:05:37.688: E/AndroidRuntime(1806): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-12 20:05:37.688: E/AndroidRuntime(1806): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-12 20:05:37.688: E/AndroidRuntime(1806): at dalvik.system.NativeStart.main(Native Method)
11-12 20:05:38.218: I/dalvikvm(1806): threadid=3: reacting to signal 3
11-12 20:05:38.228: I/dalvikvm(1806): Wrote stack traces to '/data/anr/traces.txt'
我提取的數據發佈到服務器在Firefox具有防篡改數據,這裏是一個屏幕,登錄我想在2職位劃分,這裏是屏幕: http://i.stack.imgur.com/GZiZ0.png http ://i.stack.imgur.com/FKH3P.png
我不確定是否必須發送第一頁或第二個發佈數據,可以是這個錯誤嗎?謝謝。
什麼是MainActivity類中的第93行? – kosa
92-93行是這樣的:String cookie = response.getFirstHeader(「Cookie」) .getValue(); – user1818263