Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.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
Java Android web视图未向php发送post数组?_Java_Php_Android_Webview - Fatal编程技术网

Java Android web视图未向php发送post数组?

Java Android web视图未向php发送post数组?,java,php,android,webview,Java,Php,Android,Webview,问题是我没有从android webview收到任何$u POST['registerationID'] 我在android java中有以下代码: @Override protected void onRegistered(Context context, String registrationId) { String URL_STRING = "http://mysite.org/mysite/index.php/user/notification/"; Log.i(MyTA

问题是我没有从android webview收到任何$u POST['registerationID'] 我在android java中有以下代码:

@Override
protected void onRegistered(Context context, String registrationId) {
    String URL_STRING = "http://mysite.org/mysite/index.php/user/notification/";
    Log.i(MyTAG, "onRegistered: registrationId=" + registrationId);
    // notification 
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("registrationId",registrationId));

    try{
        HttpPost httppost = new HttpPost(URL_STRING);
        httppost.setHeader("Content-Type","text/plain");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setBooleanParameter("http.protocol.expect-continue", false);
        HttpResponse response = httpclient.execute(httppost);
        Log.i("LinkPOST:", httppost.toString());
        Log.i("postData", response.getStatusLine().toString());
        HttpEntity httpEntity = response.getEntity();
        if (httpEntity != null){
            //System.out.println("Not Empty");
            String responseBody = EntityUtils.toString(httpEntity);
            System.out.println(responseBody);
        } else {
            System.out.println("Empty");
        }
    }

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

尝试使用下面的函数。它对我有用

只需为post参数填充一个HashMap

private static void post(String url, Map<String, String> params)
        throws IOException {   

    URL url;
    try {
        url = new URL(endpoint);
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException("invalid url: " + endpoint);
    }
    StringBuilder bodyBuilder = new StringBuilder();
    Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
    // constructs the POST body using the parameters
    while (iterator.hasNext()) {
        Entry<String, String> param = iterator.next();
        bodyBuilder.append(param.getKey()).append('=')
                .append(param.getValue());
        if (iterator.hasNext()) {
            bodyBuilder.append('&');
        }
    }
    String body = bodyBuilder.toString();
    Log.v(TAG, "Posting '" + body + "' to " + url);
    byte[] bytes = body.getBytes();
    HttpURLConnection conn = null;
    try {
        Log.e("URL", "> " + url);
        conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setFixedLengthStreamingMode(bytes.length);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded;charset=UTF-8");
        // post the request
        OutputStream out = conn.getOutputStream();
        out.write(bytes);
        out.close();
        // handle the response
        int status = conn.getResponseCode();
        if (status != 200) {
          throw new IOException("Post failed with error code " + status);
        }
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
  }
private静态void post(字符串url、映射参数)
抛出IOException{
网址;
试一试{
url=新url(端点);
}捕获(格式错误){
抛出新的IllegalArgumentException(“无效url:+endpoint”);
}
StringBuilder bodyBuilder=新StringBuilder();
迭代器迭代器=params.entrySet().Iterator();
//使用参数构造帖子正文
while(iterator.hasNext()){
Entry param=iterator.next();
bodyBuilder.append(param.getKey()).append('='))
.append(param.getValue());
if(iterator.hasNext()){
bodyBuilder.append('&');
}
}
字符串body=bodyBuilder.toString();
Log.v(标记“Posting'”+body+“”到“+url”);
byte[]bytes=body.getBytes();
HttpURLConnection conn=null;
试一试{
Log.e(“URL”、“>”+URL);
conn=(HttpURLConnection)url.openConnection();
连接设置输出(真);
conn.SETUSECHACHES(假);
conn.setFixedLengthStreamingMode(bytes.length);
conn.setRequestMethod(“POST”);
conn.setRequestProperty(“内容类型”,
“application/x-www-form-urlencoded;charset=UTF-8”);
//发布请求
OutputStream out=conn.getOutputStream();
out.write(字节);
out.close();
//处理响应
int status=conn.getResponseCode();
如果(状态!=200){
抛出新IOException(“Post失败,错误代码“+状态”);
}
}最后{
如果(conn!=null){
连接断开();
}
}
}
尝试更换

httppost.setHeader("Content-Type","text/plain") 

这是php代码所期望的

有关更多详细信息,请参见此问题

httppost.setHeader("Content-Type","text/plain") 
httppost.setHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8")