Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.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-来自Map的HttpURLConnection参数<;字符串,对象>;通过POST方法_Java_Android_Post_Httpurlconnection - Fatal编程技术网

Java Android-来自Map的HttpURLConnection参数<;字符串,对象>;通过POST方法

Java Android-来自Map的HttpURLConnection参数<;字符串,对象>;通过POST方法,java,android,post,httpurlconnection,Java,Android,Post,Httpurlconnection,这只是问答,意味着只是一篇维基文章。为了实现这一点,我做了很多努力。所有人都在发布不同类型的答案。我现在成功地使用了这段代码。所以我想和大家分享 您的HttpURLConnection必须位于异步任务(doInBackground)内 步骤: 在Map函数中设置参数 以以下形式生成字符串:param=val¶m2=val2 设置HttpUrlConnection OutputStream用于附加值 使用InputStream 设置参数: Map<String,Object>

这只是问答,意味着只是一篇维基文章。为了实现这一点,我做了很多努力。所有人都在发布不同类型的答案。我现在成功地使用了这段代码。所以我想和大家分享

您的
HttpURLConnection
必须位于异步任务(doInBackground)内

步骤:

  • 在Map函数中设置参数
  • 以以下形式生成字符串:
    param=val¶m2=val2
  • 设置
    HttpUrlConnection
  • OutputStream
    用于附加值
  • 使用
    InputStream
  • 设置参数:

    Map<String,Object> params = new LinkedHashMap<>();
    params.put("tag", "sda");
    
    try {
                    StringBuilder postData = new StringBuilder();
                    for (Map.Entry<String,Object> param : params.entrySet()) {
                        if (postData.length() != 0) postData.append('&');
                        postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
                        postData.append('=');
                        postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
                    }
                    String postdata = postData.toString();
                    Log.w("postData", postdata); //HERE Postdata will be 'param=val'
                    byte[] postDataBytes = postData.toString().getBytes();
    
                    //NOW, Establishes HttpConnection
                    URL url = new URL(config.URL_REGISTER);
                    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    conn.setDoOutput(true);
                    conn.getOutputStream().write(postDataBytes);
    
                    //Read the Response from Server. I use echo json_encode($response); in php file. where $response["key"] = $value;
                    BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
                    String output;
                    System.out.println("Output from Server .... \n");
                    while ((output = br.readLine()) != null) {
                        System.out.println(output);
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                    Log.e("connec()", "UnsupportedEncodingException: " + e.toString());
                } catch (ProtocolException e) {
                    e.printStackTrace();
                    Log.e("connec()", "ProtocolException: " + e.toString());
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e("connec()", "IOException: " + e.toString());
                }
    
    Map params=newlinkedhashmap();
    参数put(“标签”、“sda”);
    试一试{
    StringBuilder postData=新建StringBuilder();
    对于(Map.Entry参数:params.entrySet()){
    如果(postData.length()!=0)postData.append('&');
    append(URLEncoder.encode(param.getKey(),“UTF-8”);
    postData.append('=');
    append(URLEncoder.encode(String.valueOf(param.getValue()),“UTF-8”);
    }
    字符串postdata=postdata.toString();
    Log.w(“postData”,postData);//这里postData将是'param=val'
    字节[]postDataBytes=postData.toString().getBytes();
    //现在,建立HttpConnection
    URL=新URL(config.URL\u寄存器);
    HttpURLConnection conn=(HttpURLConnection)url.openConnection();
    conn.setRequestMethod(“POST”);
    conn.setRequestProperty(“内容类型”、“应用程序/x-www-form-urlencoded”);
    连接设置输出(真);
    conn.getOutputStream().write(postDataBytes);
    //从服务器读取响应。我在php文件中使用echo json_encode($Response);其中$Response[“key”]=$value;
    BufferedReader br=新的BufferedReader(新的InputStreamReader((conn.getInputStream()));
    字符串输出;
    System.out.println(“从服务器输出…”\n);
    而((output=br.readLine())!=null){
    系统输出打印项次(输出);
    }
    }捕获(不支持的编码异常e){
    e、 printStackTrace();
    Log.e(“connec()”,“UnsupportedEncodingException:+e.toString());
    }捕获(协议例外e){
    e、 printStackTrace();
    Log.e(“connec()”,“ProtocolException:+e.toString());
    }捕获(IOE异常){
    e、 printStackTrace();
    Log.e(“connec()”,“IOException:+e.toString());
    }
    
    问题是什么?这只是问答,意思是一篇维基文章。为了实现这一点,我做了很多努力。所有人都在发布不同类型的答案。我现在成功地使用了这段代码。所以我想与大家分享。这很好,但最好是制作它。如果params包含
    &
    字符,该怎么办?请参见此。。有什么建议吗?你能回答吗?问题是什么?这只是问答,意思是一篇维基文章。为了实现这一点,我做了很多努力。所有人都在发布不同类型的答案。我现在成功地使用了这段代码。所以我想与大家分享。这很好,但最好是制作它。如果params包含
    &
    字符,该怎么办?请参见此。。有什么建议吗?你能回答吗?