Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android上使用HttpPost发送字符串,而不使用nameValuePairs_Android_String_Http Post - Fatal编程技术网

在Android上使用HttpPost发送字符串,而不使用nameValuePairs

在Android上使用HttpPost发送字符串,而不使用nameValuePairs,android,string,http-post,Android,String,Http Post,我一直在寻找关于如何在android上使用HttpPost方法发送信息的信息,我总是看到: HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(posturl); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("Nam

我一直在寻找关于如何在android上使用HttpPost方法发送信息的信息,我总是看到:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(posturl);

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Name","Var1"));
params.add(new BasicNameValuePair("Name2","Var2"));

httppost.setEntity(new UrlEncodedFormEntity(params));    
HttpResponse resp = httpclient.execute(httppost);
HttpEntity ent = resp.getEntity();
HttpClient-HttpClient=newdefaulthttpclient();
HttpPost HttpPost=新的HttpPost(姿态);
List params=new ArrayList();
添加参数(新的BasicNameValuePair(“名称”、“变量1”);
参数添加(新的BasicNameValuePair(“名称2”、“变量2”);
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse resp=httpclient.execute(httppost);
HttpEntity ent=resp.getEntity();
问题是我不能这样做,因为我必须连接到接收XML格式字符串的资源

您知道如何在不使用
列表的情况下只发送字符串吗?上述代码可以更新为使用
StringEntity
,结果代码如下:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(posturl);


httppost.setEntity(new StringEntity("your string"));    
HttpResponse resp = httpclient.execute(httppost);
HttpEntity ent = resp.getEntity();

您可以使用JSON作为post参数。尝试引用

//向服务器发送HTTPs请求
试一试{
Log.v(“GG”,“发送服务器1-try”);
//起始线用于服务器连接/通信
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(
“10.0.0.1/abc.php”);
List nameValuePairs=新的ArrayList(1);
添加(新的BasicNameValuePair(“qrcode”,contents));
setEntity(新的UrlEncodedFormEntity(
名称(对);
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
//终端线用于服务器连接/通信
InputStream=entity.getContent();
Toast.makeText(getApplicationContext(),
“发送到服务器并成功插入mysql”,Toast.LENGTH\u LONG)
.show();
//执行HTTP Post请求
response=httpclient.execute(httppost);
entity=response.getEntity();
字符串getResult=EntityUtils.toString(实体);
Log.e(“response=”,“”+getResult);
}捕获(例外e){
Log.e(“Log_标记”,“http连接错误”
+e.toString());
}

您是如何想到上述代码以任何方式与XML相关的?另外,当您通过http发送字符串时,它将需要一个名称,因此您将得到一个名称/值对或一个命名的URL参数。好的,请澄清您真正的问题是什么。您是否有XML字符串且服务器无法接受XML字符串值?然后您必须告诉我们服务器可以接受什么格式。或者您认为名称/值对与XML有关吗?那么答案是,不,没有。服务器使用javaScrip,它读取您发送的文本,该文本是XML文件或具有该格式。所以我不能使用值对,因为我只需要发送一个字符串。但我在互联网上看到的方法只使用ValuePairs数组。现在我试着用Praful Bhatnagar回答。我想这会有用的,谢谢你的澄清。核心问题是,
POST
如何与
JavaScript
片段相关联。您可以请求JavaScript中的数据,但是否可以接收POST directy?我不这么认为。这意味着服务器会在收到之前将其传递给JavaScript,然后通过请求变量将其传递给JavaScript。在这种情况下,你在用Praful的方法浪费时间。好吧,我不知道为什么,但他的代码工作得很好:)太好了。我不能使用JSON,服务器使用JavaScript文件这个问题很久以前就得到了回答,但是你的答案是不正确的。您使用的是NameValuePairs列表,我只需要发送一个不带NameValuePairs的字符串。此方法适用于名为file_get_contents('php://input“)只有当我们使用$_POST['content']时,这才是无效的。为什么?
// Sending HTTPs Requet to Server

    try {
        Log.v("GG", "Sending sever 1 - try");
        // start - line is for sever connection/communication
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                "10.0.0.1/abc.php");

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("qrcode", contents));


        httppost.setEntity(new UrlEncodedFormEntity(
                nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        // end - line is for sever connection/communication
        InputStream is = entity.getContent();

        Toast.makeText(getApplicationContext(),
                "Send to server and inserted into mysql Successfully", Toast.LENGTH_LONG)
                .show();

        // Execute HTTP Post Request
        response= httpclient.execute(httppost);

        entity = response.getEntity();
        String getResult = EntityUtils.toString(entity);
        Log.e("response =", " " + getResult);



    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection "
                + e.toString());
    }