2

ローカルホストで実行されているjspファイルにデータを送信しようとしています。電話で[送信]をクリックすると、アプリケーションが失敗し、表示されます。残念ながら、アプリケーションは停止しました。

LoginTask:

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("");
        }
    }

LoginActivity:

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されます(Webページに表示しようとしています。電話に入力した内容:ユーザー名とログイン)。どこに問題があるのか​​わかりません...

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/ )のように、作業をさらに簡単にする多くの優れたサードパーティライブラリの1つを検討することをお勧めします。

また、例ではサーバーエンドポイントとして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 に答える