HttpPost在android中不起作用(使用php和iis 8)

时间:2014-06-27 06:49:47

标签: php android iis-7

我使用此代码从使用HTTPPOST REQUEST

在Internet上托管的服务器获取数据

我使用AsyncTask类在以下链接中调用JSONParser类中的makeHttprequest方法

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;

}

}

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

    }
}

异步任务类

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;
}

}

使用权限

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

?>

它始终没有返回任何内容。我的模拟器当时没有响应。 当我用本地wamp服务器检查它时它工作但当我尝试使用远程服务器它没有返回。

0 个答案:

没有答案