我有这段代码:
package com.problemio;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
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.TextView;
public class LoginActivity extends Activity
{
private TextView textView;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// Show form for login_email
final EditText loginEmail = (EditText) findViewById(R.id.login_email);
String name = loginEmail.getText().toString();
// Show field for password
final EditText password = (EditText) findViewById(R.id.password);
String text = password.getText().toString();
// Show button for submit
Button submit = (Button)findViewById(R.id.submit);
// Show options for create-profile and forgot-password
//readWebpage( R.layout.login) ;
submit.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
String email = loginEmail.getText().toString();
String pass = password.getText().toString();
sendFeedback(pass, email);
}
});
}
public void sendFeedback(String pass , String email)
{
Log.d( "1" , pass );
Log.d( "1" , email );
String[] params = new String[] { "url", email, pass };
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(params);
}
private class DownloadWebPageTask extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... theParams)
{
Log.d( "Inner class: " , "Doing stuff in background" );
String myUrl = theParams[0];
String myEmail = theParams[1];
String myPassword = theParams[2];
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", myEmail ));
postParameters.add(new BasicNameValuePair("password", myPassword ));
String response = "";
for (String url : theParams)
{
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try
{
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
return response;
}
@Override
protected void onPostExecute(String result)
{
Log.d( "Post execute: " , "In the post-execute method" );
textView.setText(result);
}
}
public void readWebpage(View view)
{
Log.d( "Read webpage: " , "In the read webpage method" );
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://www.problemio.com/auth/mobile_login.php" });
}
}
我得到了这个例外:
02-23 12:29:59.078: D/gralloc_goldfish(1799): Emulator without GPU emulation detected.
02-23 12:32:12.797: D/dalvikvm(1799): GC_CONCURRENT freed 204K, 4% free 9268K/9607K, paused 30ms+8ms
02-23 12:32:16.327: D/1(1799): 123
02-23 12:32:16.327: D/1(1799): hello
02-23 12:32:16.377: D/Inner class:(1799): Doing stuff in background
02-23 12:32:16.977: W/System.err(1799): java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=hello
02-23 12:32:16.977: W/System.err(1799): at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
02-23 12:32:16.987: W/System.err(1799): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
02-23 12:32:16.987: W/System.err(1799): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
02-23 12:32:16.987: W/System.err(1799): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-23 12:32:16.998: W/System.err(1799): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
02-23 12:32:16.998: W/System.err(1799): at com.problemio.LoginActivity$DownloadWebPageTask.doInBackground(LoginActivity.java:140)
02-23 12:32:17.007: W/System.err(1799): at com.problemio.LoginActivity$DownloadWebPageTask.doInBackground(LoginActivity.java:1)
02-23 12:32:17.017: W/System.err(1799): at android.os.AsyncTask$2.call(AsyncTask.java:264)
02-23 12:32:17.017: W/System.err(1799): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-23 12:32:17.017: W/System.err(1799): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-23 12:32:17.027: W/System.err(1799): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
02-23 12:32:17.037: W/System.err(1799): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-23 12:32:17.037: W/System.err(1799): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-23 12:32:17.037: W/System.err(1799): at java.lang.Thread.run(Thread.java:856)
02-23 12:32:17.057: W/System.err(1799): java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=123
02-23 12:32:17.057: W/System.err(1799): at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
02-23 12:32:17.068: W/System.err(1799): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
02-23 12:32:17.068: W/System.err(1799): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
02-23 12:32:17.068: W/System.err(1799): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-23 12:32:17.077: W/System.err(1799): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
02-23 12:32:17.077: W/System.err(1799): at com.problemio.LoginActivity$DownloadWebPageTask.doInBackground(LoginActivity.java:140)
02-23 12:32:17.087: W/System.err(1799): at com.problemio.LoginActivity$DownloadWebPageTask.doInBackground(LoginActivity.java:1)
02-23 12:32:17.087: W/System.err(1799): at android.os.AsyncTask$2.call(AsyncTask.java:264)
02-23 12:32:17.087: W/System.err(1799): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-23 12:32:17.097: W/System.err(1799): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-23 12:32:17.097: W/System.err(1799): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
02-23 12:32:17.097: W/System.err(1799): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-23 12:32:17.107: W/System.err(1799): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-23 12:32:17.107: W/System.err(1799): at java.lang.Thread.run(Thread.java:856)
02-23 12:32:17.107: D/Post execute:(1799): In the post-execute method
02-23 12:32:17.107: D/AndroidRuntime(1799): Shutting down VM
02-23 12:32:17.107: W/dalvikvm(1799): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
02-23 12:32:17.138: E/AndroidRuntime(1799): FATAL EXCEPTION: main
02-23 12:32:17.138: E/AndroidRuntime(1799): java.lang.NullPointerException
02-23 12:32:17.138: E/AndroidRuntime(1799): at com.problemio.LoginActivity$DownloadWebPageTask.onPostExecute(LoginActivity.java:164)
02-23 12:32:17.138: E/AndroidRuntime(1799): at com.problemio.LoginActivity$DownloadWebPageTask.onPostExecute(LoginActivity.java:1)
02-23 12:32:17.138: E/AndroidRuntime(1799): at android.os.AsyncTask.finish(AsyncTask.java:602)
02-23 12:32:17.138: E/AndroidRuntime(1799): at android.os.AsyncTask.access$600(AsyncTask.java:156)
02-23 12:32:17.138: E/AndroidRuntime(1799): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
02-23 12:32:17.138: E/AndroidRuntime(1799): at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 12:32:17.138: E/AndroidRuntime(1799): at android.os.Looper.loop(Looper.java:137)
02-23 12:32:17.138: E/AndroidRuntime(1799): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-23 12:32:17.138: E/AndroidRuntime(1799): at java.lang.reflect.Method.invokeNative(Native Method)
02-23 12:32:17.138: E/AndroidRuntime(1799): at java.lang.reflect.Method.invoke(Method.java:511)
02-23 12:32:17.138: E/AndroidRuntime(1799): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-23 12:32:17.138: E/AndroidRuntime(1799): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-23 12:32:17.138: E/AndroidRuntime(1799): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
替换for:
for (String url : theParams)
{
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
有了这个:
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(myUrl);
另外,您没有使用postParameters
变量。
答案 1 :(得分:1)
您正在遍历所有参数,就好像它们是网址一样:
for (String url : theParams)
实际上,只有第一个是URL。使用您已经设置的myUrl
。