duandao8607 2014-07-13 10:06
浏览 52
已采纳

使用HttpPost对象发送值

I would like to send a value to the end of a url.

ex:

if I have id=1;, I want to send this id to end of my url (to obtain id) :

www.example.com/get/id

id values ​​are different. (ex:id=2;id=3;id=4...).

is it possible ? how can I use HttpPost for this Scenario ?

I am using these functions but I always get this message :

no parametrs was sended !

inside my url :

www.example.com/get/id :

function get($id=0){

$id = (int)$id;
if(!$id) exit("no parametrs was sended !");

$trac  = $this->m_general->get('tractions' , array('id' => $id ) , true );



if(!$trac ) $resp = "-1";
else
if($trac->expired != 0  ||   $trac->cradit_end_date < date('Y-m-d'))
{
    $resp = 0;
}else
$resp = 1;

echo json_encode(array('response'=>$resp));

}


function set(){

    $data = $this->input->post('data');
    if(!$data) exit("no parametrs was sended !");


    $message = $data;
    $message = substr($message,7,-6);
    list($qr,$date,$time) = explode("&",$message);

    $insert = array(
    'qr'=>$qr ,
    'date'=>$date ,
    'time'=>$time ,
    'main'=>$message
    );

    $this->m_general->add('qr' , $insert );

}


private void postData(String valueIWantToSend) {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(this.url_server_side);
    HttpResponse response = null;
    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("data", valueIWantToSend));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        response = httpclient.execute(httppost);
        String res = EntityUtils.toString(response.getEntity());
        Log.e("Response = ", res);

        isok = 1 ;

    } catch (ClientProtocolException e) {
        e.printStackTrace();
        // TODO Auto-generated catch block
        isok = -1 ;
    } catch (IOException e) {
        e.printStackTrace();
        // TODO Auto-generated catch block
        isok = -1 ;
    }
    //Log.e("res", response.toString()) ;
}
  • 写回答

1条回答 默认 最新

  • doudongfu8006 2014-07-13 10:34
    关注

    As you mentioned above your web application expecting GET method to pass variables. In java code you are sending a POST request. You should use GET method to pass data.

    String url = "http://www.example.com/id/YOUR_ID_DATA/data/YOUR_DATA";
    
    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet(url);
    
    // add request header
    request.addHeader("User-Agent", USER_AGENT);
    HttpResponse response = client.execute(request);
    
    BufferedReader rd = new BufferedReader(
        new InputStreamReader(response.getEntity().getContent()));
    
    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
    

    Or send both id, data in a POST request and accept as a POST response from PHP side.

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.example.com");
    HttpResponse response = null;
    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("id", YOUR_ID_DATA));
        nameValuePairs.add(new BasicNameValuePair("data", YOUR_DATA));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
    

    Then in PHP Side

    $id = $_POST["id"];
    $data = $_POST["data"];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛