本文主要是介绍Android的httpclient的post方法获取服务器端的数据(服务器端是get方法),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.首先建立一个StreamTools类:public class StreamTools {/**
*
*
* @param is
* @return
*/
public static String streamToStr(InputStream is) {
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
int len = 0;
byte buffer[] = new byte[1024];
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
is. close();
os.close();
byte data[] = os.toByteArray();
return new String(data, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
2、然后是获取服务器端数据。
package com.example.httpposttest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText userId,pwd;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
userId = (EditText) findViewById(R.id.et_un);
pwd = (EditText) findViewById(R.id.et_psd);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
HttpClient httpClient = new DefaultHttpClient();
String httpUrl = "http://115.28.26.84:8095/PersonalPage/login.aspx";
//建立一个数组链表,然后添加参数
ArrayList<NameValuePair>params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("userid", userId.getText().toString()));
params.add(new BasicNameValuePair("psd", pwd.getText().toString()));
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params,"utf-8");
InputStream is = entity.getContent();
//str得出的是后面的参数。str="userid=654&psd=123"
String str = StreamTools.streamToStr(is);
httpUrl +="?"+ str;
HttpPost httpPost = new HttpPost(httpUrl);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
String result="";
if(response.getStatusLine().getStatusCode()==200){
result = EntityUtils.toString(response.getEntity(),"utf-8");
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putCharSequence("result", result);
intent.putExtras(bundle);
intent.setClass(MainActivity.this, ResultTest.class);
startActivity(intent);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
httpClient.getConnectionManager().shutdown();
}
}).start();
}
});
}
}
这篇关于Android的httpclient的post方法获取服务器端的数据(服务器端是get方法)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!