从Android到PHP页面的HttpPost请求(参数未传递)

从Android到PHP页面的HttpPost请求(参数未传递),php,android,parameters,http-post,Php,Android,Parameters,Http Post,我的PHP代码: <?php $data = 'basic'; if( $_POST["tag"]){ $data = $data.$_POST['tag']; } if( $_GET["tag"]){ $data = $data.$_GET['tag']; } echo $data; ?> 公共JSONObject makeHttpRequest(字符串url、字符串方法、, 列表参数){ //发出HTTP请求 试一试{ //检查请求方法 如果(方法==“POS

我的PHP代码:

<?php
$data = 'basic';

if( $_POST["tag"]){
   $data = $data.$_POST['tag'];
}

if( $_GET["tag"]){
   $data = $data.$_GET['tag'];
}

echo $data;
?>
公共JSONObject makeHttpRequest(字符串url、字符串方法、, 列表参数){ //发出HTTP请求 试一试{ //检查请求方法 如果(方法==“POST”){ //现在是defaultHttpClient对象 DefaultHttpClient httpClient=新的DefaultHttpClient(); HttpPost HttpPost=新的HttpPost(url); setEntity(新的UrlEncodedFormEntity(参数)); HttpResponse HttpResponse=httpClient.execute(httpPost); HttpEntity HttpEntity=httpResponse.getEntity(); is=httpEntity.getContent(); }else if(方法==“GET”){ //请求方法是GET DefaultHttpClient httpClient=新的DefaultHttpClient(); String paramString=URLEncodedUtils.format(params,“utf-8”); url+=“?”+参数字符串; HttpGet HttpGet=新的HttpGet(url); HttpResponse HttpResponse=httpClient.execute(httpGet); HttpEntity HttpEntity=httpResponse.getEntity(); is=httpEntity.getContent(); } }捕获(不支持的编码异常e){ e、 printStackTrace(); }捕获(客户端协议例外e){ e、 printStackTrace(); }捕获(IOE异常){ e、 printStackTrace(); } //下面是将读取并转换为字符串,然后将其传递给主活动 试一试{ BufferedReader reader=新的BufferedReader(新的InputStreamReader( is,“iso-8859-1”),8); StringBuilder str=新的StringBuilder(); 字符串strLine=null; 而((strLine=reader.readLine())!=null){ str.append(strLine+“\n”); } is.close(); json=str.toString(); }捕获(例外e){ } //现在,我们将尝试将字符串解析为JSON对象 试一试{ jsonObj=新的JSONObject(json); }捕获(JSONException e){ } 返回jsonObj; }
与我在问题中提到的代码相同。它不起作用
String url = http: //<IP>/sreeweb/sample.php;
List params = new ArrayList();
params.add(new BasicNameValuePair("tag", "services"));

InputStream is = null;

try {

   DefaultHttpClient httpClient = new DefaultHttpClient();
   HttpPost httpPost = new HttpPost(url);
   httpPost.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded");
   httpPost.setHeader("Accept", "*/*");
   httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
   HttpResponse httpResponse = httpClient.execute(httpPost);
   Log.d("msg", "res : " + httpResponse.getStatusLine().getStatusCode()); //200

   HttpEntity httpEntity = httpResponse.getEntity();

   is = httpEntity.getContent();
   Log.d("msg", "" + is);
} catch (UnsupportedEncodingException e) {
   e.printStackTrace();
} catch (ClientProtocolException e) {
   e.printStackTrace();
} catch (IOException e) {
   e.printStackTrace();
}

BufferedReader reader;
StringBuilder stringBuilder = null;

try {
   reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);

   stringBuilder = new StringBuilder();
   String line = null;

   while ((line = reader.readLine()) != null) {
      stringBuilder.append(line + "\n");
   }
   is.close();

} catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}

String result = stringBuilder.toString();
Log.e(TAG, result);
try {

   String data = URLEncoder.encode("tag", "UTF-8") + "=" + URLEncoder.encode("services", "UTF-8");
   URL url = new URL(finalUrl);
   HttpURLConnection conn = null;
   conn = (HttpURLConnection) url.openConnection();
           conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
   conn.setRequestProperty("Accept", "*/*");
   // Allow Inputs
   conn.setDoInput(true);
   conn.setDoOutput(true);
   // Use a post method.
   conn.setRequestMethod("POST");

   OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
   wr.write(data);
   wr.flush();

   BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

   StringBuilder sb = new StringBuilder();
   String line = null;

   // Read Server Response
   while ((line = reader.readLine()) != null) {
      sb.append(line);
      break;
   }

   Log.e(TAG, "postMethod  " + sb.toString());
   return sb.toString();


} catch (Exception e) {
    return new String("Exception: " + e.getMessage());
}
public JSONObject makeHttpRequest(String url, String method,
        List<NameValuePair> params) {

    // Make HTTP request
    try {

        // checking request method
        if (method == "POST") {

            // now defaultHttpClient object
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } else if (method == "GET") {
            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //HERE is WILL READ AND CONVERTED INTO STRING THEN PASS IT TO MAIN ACTIVITY
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder str = new StringBuilder();
        String strLine = null;
        while ((strLine = reader.readLine()) != null) {
            str.append(strLine + "\n");
        }
        is.close();
        json = str.toString();
    } catch (Exception e) {

    }

    // now will try to parse the string into JSON object
    try {
        jsonObj = new JSONObject(json);
    } catch (JSONException e) {

    }

    return jsonObj;

}