发送数据的android将jsp [英] Send data to jsp from android
问题描述
i'm试图将数据发送到本地主机上运行JSP文件。当我点击提交我的手机上,该应用程序失败,并显示出我的不幸的是,应用程序已停止。的
i´m trying to send data to a jsp file running on localhost.. When i click submit on my phone, the application fails, and shows me Unfortunately, application has stopped.
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)显示我空
(i'm试图表明的网页,我输入我的手机:用户名和登录)。我不知道是哪里出了问题......
When i hit Submit on my phone, it shows me Error(Toast). The jsp page(AndroidCheck.jsp) shows me null
(i´m trying to show on webpage, what i type in my phone:username and login). I have no idea, where is the problem...
推荐答案
这里的问题改变了方向几次(从GET至POST)。在努力营造一个简单而广泛的回答可以帮助你,我已经包含下面的一个例子都可以做一个POST,并通过的HttpClient
一个GET在Android(每使用的AsyncTask
)。
The question here changed direction a few times (from GET to POST). In an effort to create a simple but broad answer that might help you, I've included an EXAMPLE below that can do both a POST and a GET via HttpClient
on Android (each using an AsyncTask
).
请记住这个例子是,不要做HTTP在Android的唯一途径,而且是过度简化了几个地方。这是为了让您开始使用的HttpClient
。
Keep in mind this example ISN'T the only way to do HTTP on Android, and is oversimplified in a few places. It's intended to get you started with HttpClient
.
有替代HTTP客户端,其中只使用 java.net
在某些情况下,看到这篇博客文章的详细信息:的 http://android-developers.blogspot.com/2011/09/androids-http-clients.html 一>。
There are alternative HTTP clients, including just using java.net
in some scenarios, see this blog post for more info: http://android-developers.blogspot.com/2011/09/androids-http-clients.html.
此外,你可能要考虑很多很好的第三方库,使东西更容易,就像循环J的异步安卓HTTP客户端之一:的 http://loopj.com/android-async-http/ 。
Also, you may want to consider one of the many good third party libraries that make stuff even easier, like loopj's Asynchronous Android Http Client: http://loopj.com/android-async-http/.
和,请注意,我用 http://httpbin.org/ 在本例中为服务器端点。 httpbin.org作为它可以让您发送和检查各种HTTP数据的一个伟大的测试服务器。
And, note that I've used http://httpbin.org/ in the example as a server endpoint. httpbin.org serves as a great test server that let's you send and check all kinds of HTTP data.
活动
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>
这篇关于发送数据的android将jsp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!