2

我正在尝试将数据发送到在本地主机上运行的 jsp 文件。当我在手机上单击提交时,应用程序失败,并向我显示不幸的是,应用程序已停止。

登录任务:

public class LoginTask extends AsyncTask<String, Void, Integer> {

    private ProgressDialog progressDialog;
    private LoginActivity activity;
    private int id = -1;
        public LoginTask(LoginActivity activity, ProgressDialog progressDialog)
        {
            this.activity = activity;
            this.progressDialog = progressDialog;
        }

        @Override
        protected void onPreExecute()
        {
            progressDialog.show();
        }

        @Override
        protected Integer doInBackground(String... arg0) 
        {
            String result = "";
            int responseCode = 0;
            try 
            {
                HttpClient client = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://192.168.1.100:8080/test/AndroidCheck.jsp");

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("username", arg0[0]));
                    nameValuePairs.add(new BasicNameValuePair("password", arg0[1]));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                int executeCount = 0;
                HttpResponse response;
                do
                {
                    progressDialog.setMessage("Logging in.. ("+(executeCount+1)+"/5)");
                                    executeCount++;
                    response = client.execute(httppost);
                    responseCode = response.getStatusLine().getStatusCode();                        
                                } while (executeCount < 5 && responseCode == 408);

                BufferedReader rd = new BufferedReader(new InputStreamReader(
                        response.getEntity().getContent()));

                String line;
                while ((line = rd.readLine()) != null)
                {
                    result = line.trim();
                }
                id = Integer.parseInt(result);
            }
            catch (Exception e) {
                responseCode = 408;
                e.printStackTrace();
            }
            return responseCode;
        }

        @Override
        protected void onPostExecute(Integer headerCode)
        {
            progressDialog.dismiss();
            if(headerCode == 202)
                activity.login(id);
            else
                activity.showLoginError("");
        }
    }

登录活动:

public class LoginActivity extends Activity {
    protected static final int LOGIN_REQUEST_CODE = 0;
    protected static final int RECOVER_REQUEST_CODE = 1;
    protected static final int REGISTER_REQUEST_CODE = 2;
    public static final int LOGOUT_RESULT_CODE = 2;

    SharedPreferences sharedPreferences;
    private EditText username;
     private EditText password;
      private Button btnLogin;;

        private final Class<?> LOGIN_DESTINATION = AndroidNavigationTabsActivity.class; 

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {

        sharedPreferences = getPreferences(MODE_PRIVATE);

        super.onCreate(savedInstanceState);


        if( sharedPreferences.getBoolean("user_logged_in", false))
        {

            startActivityForResult(
                new Intent(LoginActivity.this, LOGIN_DESTINATION), 
                LOGIN_REQUEST_CODE);
        }

        setContentView(R.layout.login_activity);
        username=(EditText)this.findViewById(R.id.username);
        password=(EditText)this.findViewById(R.id.password);
        btnLogin=(Button)this.findViewById(R.id.btnLogin);

        btnLogin.setOnClickListener(loginOnClickListener);
    }

    protected OnClickListener loginOnClickListener = new OnClickListener()
    {
        public void onClick(View v) 
        {
            ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this);
            progressDialog.setMessage("Logging in...");
            progressDialog.setCancelable(false);

            LoginTask loginTask = new LoginTask(LoginActivity.this, progressDialog);
            loginTask.execute(username.getText().toString(),password.getText().toString());
        }
    };

    public void showLoginError(String result)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
        builder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        builder.setMessage(R.string.login_invalid_error);
        AlertDialog alert = builder.create();
        alert.setCancelable(false);
        alert.show();
    }

    public void login(int id)
    {
        sharedPreferences.edit().putBoolean("user_logged_in", true).commit();
        startActivityForResult(
            new Intent(LoginActivity.this, LOGIN_DESTINATION), 
            LOGIN_REQUEST_CODE);
    }
}

当我在手机上点击提交时,它显示错误(吐司)。jsp页面(AndroidCheck.jsp)向我展示null(我试图在网页上显示,我在手机中输入的内容:用户名和登录名)。我不知道,问题出在哪里...

4

1 回答 1

1

这里的问题改变了几次方向(从 GET 到 POST)。为了创建一个可能对您有所帮助的简单但广泛的答案,我在下面提供了一个示例,它可以HttpClient在 Android 上同时执行 POST 和 GET(每个都使用AsyncTask)。

请记住,此示例并不是在 Android 上执行 HTTP 的唯一方法,并且在某些地方过于简化。它旨在让您开始使用HttpClient.

有其他 HTTP 客户端,包括仅java.net在某些场景中使用,请参阅此博客文章了解更多信息:http ://android-developers.blogspot.com/2011/09/androids-http-clients.html 。

此外,您可能需要考虑使用许多优秀的第三方库之一,这些库可以让事情变得更加简单,例如 loopj 的异步 Android Http 客户端:http: //loopj.com/android-async-http/

并且,请注意,我在示例中使用了http://httpbin.org/作为服务器端点。httpbin.org 是一个很好的测试服务器,可以让您发送和检查各种 HTTP 数据。

活动

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.client.methods.HttpRequestBase;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;

public class MainActivity extends Activity {

   private ProgressDialog dialog;

   private Button get;
   private Button post;
   private TextView output;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  output = (TextView) findViewById(R.id.output);

  get = (Button) findViewById(R.id.get_button);
  post = (Button) findViewById(R.id.post_button);

  get.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
        output.setText("");
        new SimpleHttpGetTask(MainActivity.this).execute("http://httpbin.org/get");
     }
  });

  post.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
        output.setText("");
        new SimpleHttpPostTask(MainActivity.this).execute("http://httpbin.org/post", "param1name", "param1Value");
     }
  });
}

@Override
protected void onPause() {
  super.onPause();
  if (dialog != null) {
     dialog.dismiss();
  }
}

private class SimpleHttpGetTask extends AsyncTask<String, Void, String> {

  private final Context context;

  public SimpleHttpGetTask(Context context) {
     this.context = context;
     dialog = new ProgressDialog(context);
  }

  @Override
  protected void onPreExecute() {
     dialog.setMessage("doing a GET...");
     dialog.show();
  }

  @Override
  protected String doInBackground(String... args) {

     if (args == null || args.length != 1) {
        Log.w("TAG", "args size must be 1: URL to invoke");
        return null;
     }

     String url = args[0];
     Log.d("TAG", "hitting URL:" + url);

     // AndroidHttpClient has more reasonable default settings than "DefaultHttpClient", but it was only added in API 8
     // (if you need to support API levels lower than 8, I'd create your own HttpClient with similar settings [see bottom of page for example])
     AndroidHttpClient client = AndroidHttpClient.newInstance("Android-MyUserAgent");

     HttpRequestBase request = new HttpGet(url);
     HttpResponse response = null;

     try {
        response = client.execute(request);

        int code = response.getStatusLine().getStatusCode();
        String message = response.getStatusLine().getReasonPhrase();

        // use the response here
        // if the code is 200 thru 399 it worked or was redirected
        // if the code is >= 400 there was a problem

        Log.d("TAG", "http GET completed, code:" + code + " message:" + message);

        HttpEntity entity = response.getEntity();
        if (entity != null) {
           InputStream instream = null;
           try {
              instream = entity.getContent();

              // convert stream if gzip header present in response
              Header contentEncoding = response.getFirstHeader("Content-Encoding");
              if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                 instream = new GZIPInputStream(instream);
              }

              // returning the string here, but if you want return the code, whatever
              String result = convertStreamToString(instream);
              Log.d("TAG", "http GET result (as String):" + result);
              return result;

           } finally {
              if (instream != null) {
                 instream.close();
              }
           }
        }
     } catch (Exception e) {
        Log.e("TAG", "Error with HTTP GET", e);
     } finally {
        client.close();
     }

     return null;
  }

  @Override
  protected void onPostExecute(String result) {
     dialog.dismiss();

     if (result != null) {
        // use response back on UI thread here
        output.setText(result);
     }
  }
}

private class SimpleHttpPostTask extends AsyncTask<String, Void, String> {

  private final Context context;

  public SimpleHttpPostTask(Context context) {
     this.context = context;
     dialog = new ProgressDialog(context);
  }

  @Override
  protected void onPreExecute() {
     dialog.setMessage("doing a POST...");
     dialog.show();
  }

  @Override
  protected String doInBackground(String... args) {

     if (args == null || args.length != 3) {
        // in real use you might want to make a better request class of your own to pass in, instead of using a bunch of strings
        Log.w("TAG", "args size must be 3: URL to invoke, and 1st param (name/value) to submit");
        return null;
     }

     String url = args[0];
     String param1Name = args[1];
     String param1Value = args[2];
     Log.d("TAG", "hitting URL:" + url);

     // AndroidHttpClient has more reasonable default settings than "DefaultHttpClient", but it was only added in API 8
     // (if you need to support API levels lower than 8, I'd create your own HttpClient with similar settings [see bottom of page for example])
     AndroidHttpClient client = AndroidHttpClient.newInstance("Android-MyUserAgent");

     HttpRequestBase request = new HttpPost(url);
     HttpResponse response = null;

     try {
        // there are different kinds of POSTS, url encoded form is ONE 
        // (which you need depends on what you're trying to post and how the server is implemented)
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair(param1Name, param1Value));
        ((HttpPost) request).setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

        response = client.execute(request);

        int code = response.getStatusLine().getStatusCode();
        String message = response.getStatusLine().getReasonPhrase();

        // use the response here
        // if the code is 200 thru 399 it worked or was redirected
        // if the code is >= 400 there was a problem

        Log.d("TAG", "http POST completed, code:" + code + " message:" + message);

        HttpEntity entity = response.getEntity();
        if (entity != null) {
           InputStream instream = null;
           try {
              instream = entity.getContent();

              // convert stream if gzip header present in response
              Header contentEncoding = response.getFirstHeader("Content-Encoding");
              if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                 instream = new GZIPInputStream(instream);
              }

              // returning the string here, but if you want return the code, whatever
              String result = convertStreamToString(instream);
              Log.d("TAG", "http GET result (as String):" + result);
              return result;

           } finally {
              if (instream != null) {
                 instream.close();
              }
           }
        }
     } catch (Exception e) {
        Log.e("TAG", "Error with HTTP POST", e);
     } finally {
        client.close();
     }
     return null;
  }

  @Override
  protected void onPostExecute(String result) {
     dialog.dismiss();
     if (result != null) {
        // use response back on UI thread here
        output.setText(result);
     }
  }
}

// this is OVERSIMPLE (for the example), in the real world, if you have big responses, use the stream 
private static String convertStreamToString(InputStream is) {
  try {
     return new java.util.Scanner(is, "UTF-8").useDelimiter("\\A").next();
  } catch (java.util.NoSuchElementException e) {
     return "";
  }
}

/*
// ONE example of some default settings for DefaultHttpClient (if you can't use AndroidHttpClient, or want more fine grained control)
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 15000);
HttpConnectionParams.setSoTimeout(params, 20000);
HttpConnectionParams.setStaleCheckingEnabled(params, false);
HttpConnectionParams.setSocketBufferSize(params, 8192);
HttpClientParams.setRedirecting(params, false);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
HttpClient client = new DefaultHttpClient(params);
*/
}

布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical"
android:padding="10dp" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="HTTP BASICS" />

<Button
    android:id="@+id/get_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="HTTP GET" />

<Button
    android:id="@+id/post_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="HTTP POST" />

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:id="@+id/output"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</ScrollView>

显现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.com.totsp.httptest"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.com.totsp.httptest.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>
</application>

在此处输入图像描述

于 2013-03-09T16:07:08.247 回答