HttpPost在andorid中不工作(使用php和iis 8)
我使用这段代码通过HTTPPOST请求从托管在internet上的服务器获取数据 在下面的链接中,我使用AsyncTask类调用JSONParser类中的makeHttprequest方法HttpPost在andorid中不工作(使用php和iis 8),php,android,iis-7,Php,Android,Iis 7,我使用这段代码通过HTTPPOST请求从托管在internet上的服务器获取数据 在下面的链接中,我使用AsyncTask类调用JSONParser类中的makeHttprequest方法 public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; public JSONObject makeHttpRequest(String u
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
try {
if (method == "POST") {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(
params);
httpPost.setEntity(urlEncodedFormEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} else if (method == "GET") {
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
Log.d("get_param", paramString);
url += "?" + paramString;
Log.d("get_url", url);
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.d("unsupportedencodingexception",
"UnsupportedEncodingException");
} catch (ClientProtocolException e) {
e.printStackTrace();
Log.d("clientprotocolexception", "ClientProtocolException");
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
Log.d("json_String", json.toString());
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
公共类JSONParser{
静态InputStream为空;
静态JSONObject jObj=null;
静态字符串json=“”;
公共JSONObject makeHttpRequest(字符串url、字符串方法、,
列表参数){
试一试{
如果(方法==“POST”){
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
UrlEncodedFormEntity UrlEncodedFormEntity=新UrlEncodedFormEntity(
参数);
setEntity(urlEncodedFormEntity);
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}else if(方法==“GET”){
DefaultHttpClient httpClient=新的DefaultHttpClient();
String paramString=URLEncodedUtils.format(params,“utf-8”);
Log.d(“get_param”,paramString);
url+=“?”+参数字符串;
Log.d(“获取url”,url);
HttpGet HttpGet=新的HttpGet(url);
HttpResponse HttpResponse=httpClient.execute(httpGet);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}
}捕获(不支持的编码异常e){
e、 printStackTrace();
Log.d(“unsupportedencodingexception”,
“不支持的编码例外情况”);
}捕获(客户端协议例外e){
e、 printStackTrace();
Log.d(“clientprotocolexception”、“clientprotocolexception”);
}捕获(非法状态){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
is,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
json=sb.toString();
Log.d(“json_字符串”,json.toString());
}捕获(例外e){
Log.e(“缓冲区错误”,“错误转换结果”+e.toString());
}
试一试{
jObj=新的JSONObject(json);
}捕获(JSONException e){
Log.e(“JSON解析器”,“错误解析数据”+e.toString());
}
返回jObj;
}
}
调用makehttprequest
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
url = (EditText) findViewById(R.id.url);
t = (TextView) findViewById(R.id.text);
usernameEditText = (EditText) findViewById(R.id.login_username_editText);
passwordEditText = (EditText) findViewById(R.id.login_password_editText);
sendPostReqButton = (Button) findViewById(R.id.login_sendPostReq_button);
sendPostReqButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.login_sendPostReq_button) {
String givenUsername = usernameEditText.getEditableText()
.toString();
String givenPassword = passwordEditText.getEditableText()
.toString();
System.out.println("Given username :" + givenUsername
+ " Given password :" + givenPassword);
String url = "http://xx.xx.xx.xx/login.php";
BasicNameValuePair usernameBasicNameValuePair = new BasicNameValuePair("paramUsername", paramUsername);
BasicNameValuePair passwordBasicNameValuePAir = new BasicNameValuePair(
"paramPassword", paramPassword);
List<NameValuePair> params = newArrayList<NameValuePair>();
params.add(usernameBasicNameValuePair);
params.add(passwordBasicNameValuePAir);
JSONObject jsonObject = null;
try {
jsonObject = new Async(Customerdetails.this,
url,"POST")
.execute(params).get();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ExecutionException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
String success = jsonObject.getString("success");
if (success.equals("working") && success != null) {
Toast.make(this,""+success,Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
url=(EditText)findViewById(R.id.url);
t=(TextView)findViewById(R.id.text);
usernameEditText=(EditText)findViewById(R.id.login\u username\u EditText);
passwordEditText=(EditText)findViewById(R.id.login\u password\u EditText);
sendPostReqButton=(按钮)findViewById(R.id.login\u sendPostReq\u按钮);
sendPostReqButton.setOnClickListener(此);
}
@凌驾
公共void onClick(视图v){
if(v.getId()==R.id.login\u sendPostReq\u按钮){
字符串givenUsername=usernameEditText.getEditableText()
.toString();
字符串givenPassword=passwordEditText.getEditableText()
.toString();
System.out.println(“给定用户名:+givenUsername
+“给定密码:”+givenPassword);
字符串url=”http://xx.xx.xx.xx/login.php";
BasicNameValuePair用户名BasicNameValuePair=新的BasicNameValuePair(“paramUsername”,paramUsername);
BasicNameValuePair密码BasicNameValuePair=新的BasicNameValuePair(
“paramPassword”,paramPassword);
List params=newArrayList();
params.add(usernamecnamevaluepair);
params.add(passwordBasicNameValuePAir);
JSONObject JSONObject=null;
试一试{
jsonObject=new Async(Customerdetails.this,
url,“发布”)
.execute(params.get();
}捕捉(中断异常e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}捕获(执行异常e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
试一试{
String success=jsonObject.getString(“success”);
if(success.equals(“working”)&&success!=null){
Toast.make(这个“+”成功,Toast.LENGTH_SHORT.show();
}
}catch(JSONException e){//TODO自动生成的catch块
e、 printStackTrace();
}
}
}
异步任务类
public class Async extends
AsyncTask<List<NameValuePair>, String, JSONObject> {
ProgressDialog progressDialog;
Context c;
String method, url;
public SubmitAsync(Context c, String url, String method) {
// TODO Auto-generated constructor stub
this.c = c;
this.method = method;
this.url = url;
}
@Override
protected void onPostExecute(JSONObject result) {
// TODO Auto-generated method stub
progressDialog.dismiss();
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog = new ProgressDialog(c);
progressDialog.setMessage("Uploading Data...");
progressDialog.setIndeterminate(true);
progressDialog.show();
}
@Override
protected JSONObject doInBackground(List<NameValuePair>... params) {
// TODO Auto-generated method stub
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = jsonParser.makeHttpRequest(url, method,
params[0]);
return jsonObject;
}
公共类异步扩展
异步任务{
进行对话进行对话;
上下文c;
字符串方法,url;
公共SubmitAsync(上下文c、字符串url、字符串方法){
//TODO自动生成的构造函数存根
这个.c=c;
这个方法=方法;
this.url=url;
}
@凌驾
受保护的void onPostExecute(JSONObject结果){
//TODO自动生成的方法存根
progressDialog.disclose();
super.onPostExecute(结果);
}
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
super.onPreExecute();
progressDialog=新建progressDialog(c);
progressDialog.setMessage(“上传数据…”);
progressDialog.setUndeterminate(true);
progressDialog.show();
}
@凌驾
受保护的JSONObject doInBackground(列表…参数){
//TODO自动生成的方法存根
JSONParser JSONParser=新的JSONParser();
JSONObject JSONObject=jsonParser.makeHttpRequest(url,方法,
参数[0]);
返回jsonObject;
}
}
使用权限
<uses-permission android:name="android.permission.INTERNET" />
我使用这个php文件
<?php
$varUsername = $_POST['paramUsername'];
$varPassword = $_POST['paramPassword'];
$success = array();
if($varUsername == "anuja" && $varPassword == "123"){
$success['success'] = 'working';
echo json_encode($success);
}else{
$success['success'] = 'invalid';
echo json_encode($success);
}
?>
它总是不返回任何内容。我的模拟器没有响应