2015-10-17 40 views
0

我有一些代碼通過POST方法的一些字符串參數發送到服務器 我的代碼是這樣的:如何發佈參數

public int uploadAghahi(aghahi AghahiToSend) { 

     HttpURLConnection conn = null; 
     DataOutputStream dos = null; 
     String lineEnd = "\r\n"; 
     String twoHyphens = "--"; 
     String boundary = "*****"; 
     int bytesRead, bytesAvailable, bufferSize; 
     byte[] buffer; 
     int maxBufferSize = 1 * 1024 * 1024; 
     File sourceFile = new File(AghahiToSend.imagePath); 

     if (!sourceFile.isFile()) { 

      prgDialog.dismiss(); 

      runOnUiThread(new Runnable() { 
       public void run() { 
       } 
      }); 

      return 0; 

     } else { 
      try { 
       JSONObject params = new JSONObject(); 
       params.put("mainsubjectid", "1"); 
       params.put("subsubjectid", "22"); 
       params.put("stateid", "144"); 
       params.put("cityid", "144"); 
       params.put("onvan", "سیاسیباسیباشیباشسیباسیبا"); 
       params.put("address", "سیاسیباسیباشیباشسیباسیبا"); 
       params.put("phone", "سیاسیباسیباشیباشسیباسیبا"); 
       params.put("email", "[email protected]"); 
       params.put("tozihat", "سیاسیباسیباشیباشسیباسیبا"); 

       FileInputStream fileInputStream = new FileInputStream(
         sourceFile); 
       URL url = new URL(ADDRESSsendAghahi); 
       conn = (HttpURLConnection) url.openConnection(); 
       conn.setRequestMethod("POST"); 
       conn.setRequestProperty("Accept-Charset", "UTF-8"); 
       conn.setDoInput(true); // Allow Inputs 
       conn.setDoOutput(true); // Allow Outputs 
       conn.setUseCaches(false); // Don't use a Cached Copy 

       conn.setRequestProperty("Connection", "Keep-Alive"); 
       conn.setRequestProperty("ENCTYPE", "multipart/form-data"); 
       conn.setRequestProperty("Content-Type", 
         "multipart/form-data;boundary=" + boundary); 
       conn.setRequestProperty("uploaded_file", AghahiToSend.imagePath); 
       // conn.setRequestProperty("content-type", 
       // "application/json;charset=UTF-8"); 

       dos = new DataOutputStream(conn.getOutputStream()); 

       dos.writeBytes(twoHyphens + boundary + lineEnd); 

       dos.writeBytes("Content-Type: text/plain; charset=UTF-8" 
         + lineEnd); 
       dos.writeBytes("Content-Disposition: form-data; name=\"obj\"" 
         + lineEnd); 
       dos.writeBytes(lineEnd); 
       dos.writeBytes(params.toString()); 
       dos.writeBytes(lineEnd); 
       dos.writeBytes(twoHyphens + boundary + lineEnd); 


       // Json_Encoder encode=new Json_Encoder(); 
       // call to encode method and assigning response data to variable 
       // 'data' 
       // String data=encode.encod_to_json(); 
       // response of encoded data 
       // System.out.println(data); 

       // Adding Parameter filepath 

       dos.writeBytes(twoHyphens + boundary + lineEnd); 

       dos.writeBytes("Content-Disposition: form-data; name=\"filepath\"" 
         + lineEnd); 
       // dos.writeBytes("Content-Type: text/plain; charset=UTF-8" 
       // + lineEnd); 
       // dos.writeBytes("Content-Length: " + name.length() + lineEnd); 
       dos.writeBytes(lineEnd); 
       dos.writeBytes(AghahiToSend.imagePath); // mobile_no is String 
                 // variable 
       dos.writeBytes(lineEnd); 

       // Adding Parameter media file(audio,video and image) 

       dos.writeBytes(twoHyphens + boundary + lineEnd); 

       dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" 
         + AghahiToSend.imagePath + "\"" + lineEnd); 
       dos.writeBytes(lineEnd); 

       // create a buffer of maximum size 
       bytesAvailable = fileInputStream.available(); 
       bufferSize = Math.min(bytesAvailable, maxBufferSize); 
       buffer = new byte[bufferSize]; 
       // read file and write it into form... 
       bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

       while (bytesRead > 0) { 
        dos.write(buffer, 0, bufferSize); 
        bytesAvailable = fileInputStream.available(); 
        bufferSize = Math.min(bytesAvailable, maxBufferSize); 
        bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
       } 

       // send multipart form data necesssary after file data... 
       dos.writeBytes(lineEnd); 
       dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

       serverResponseCode = conn.getResponseCode(); 
       serverResponseMessage = conn.getResponseMessage(); 

       InputStream is = conn.getInputStream(); 
       BufferedReader reader = new BufferedReader(
         new InputStreamReader(is)); 
       String line = ""; 
       while ((line = reader.readLine()) != null) { 
        result += line; 
       } 
       Toast.makeText(AddAghahiActivity.this, serverResponseMessage, 
         Toast.LENGTH_LONG).show(); 
       if (serverResponseCode == 200) { 

        runOnUiThread(new Runnable() { 
         public void run() { 
         } 
        }); 
       } 

       // close the streams // 
       fileInputStream.close(); 
       dos.flush(); 
       dos.close(); 

      } catch (MalformedURLException ex) { 
        } 
       }); 
      } catch (final Exception e) { 
        } 
       }); 
      } 
      prgDialog.dismiss(); 
      return serverResponseCode; 
     } 
    } 

我有一個問題,它是與編碼字符串。 一些我的琴絃都在波斯,當發送到服務器,他們得到changet爲其他字符象下面這樣:

,3ED3ED3~'3E'3ED4 

*G1'F 

我怎麼能correctlly發送PARAMS?

回答

0

找到合適的服務器編碼,並使用該編碼對您的請求進行編碼,然後使用該編碼解碼響應。例如,我昨天在Python上編寫的代碼如下:

params = urllib.parse.urlencode({'tool': tool, 'input': full_text, 'token': token}).encode("UTF-8") # Encoding parameters 
result = urllib.request.urlopen(api_url, params) # Making request 
readed_result = result.read().decode("UTF-8") # Decoding response 

Java中的邏輯也是一樣的。

如何找到合適的服務器編碼?檢查http頭以進行編碼。 例如:

enter image description here