2013-09-27 97 views
0

我已經嘗試了很多不同的解決方案,以一個XML文件發佈到PHP服務器。發送XML到PHP服務器與Android

的響應始終爲 「十月9日至27日:49:10.550:I/TAG(3950):文件MIME-TYPE無法識別」。

代碼的最後一個版本是:

httppost.setHeader("Content-Type","text/xml;charset=\"UTF-8\""); 

    String textToUpload = ""; 
    try { 
     textToUpload = getStringFromFile(fileName); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return new StringEntity(textToUpload); 

之前有這樣的連接的初始化:

private void openConnection() throws IOException { 
    httpclient = new DefaultHttpClient(); 
    httppost = new HttpPost(url); 
} 

,響應被以這種方式處理後:

private int getServerResponse() throws IOException { 
    HttpResponse response = httpclient.execute(httppost); 
    Log.i("CIAO", EntityUtils.toString(response.getEntity())); 
    StatusLine statusLine = response.getStatusLine(); 
    return statusLine.getStatusCode(); 
} 

我自己也嘗試這種解決方案(和很多不同的人),但沒有成功:

File file = new File(fileName); 
    httppost.setHeader("Content-Type","text/xml;charset=UTF-8"); 

    ContentBody fb = new FileBody(file, "text/xml"); 
    MultipartEntity entity = new MultipartEntity(
      HttpMultipartMode.STRICT); 
    entity.addPart("file", fb); 
    httppost.setEntity(entity); 

任何想法?

回答

0

我已經解決了這個問題是這樣的:

File file = new File(fileName); 

    ContentBody fb = new FileBody(file, "text/xml"); 
    MultipartEntity entity = new MultipartEntity(
      HttpMultipartMode.BROWSER_COMPATIBLE); 
    entity.addPart("file", fb); 
    httppost.setEntity(entity); 
    return entity; 

我不知道這個問題是否嚴格或頁眉設置..反正現在它的工作原理.. :)