2014-02-25 101 views
0

我上傳我的json文件到php服務器,但它給了我文件未找到。我已經動態地創建了它,並將它顯示在設備中。我給了同樣的道路。我指的是https://coderwall.com/p/zjuwbg在android中上傳json文件到php服務器

代碼

 final String uploadFilePath = Environment.getExternalStorageDirectory().getAbsolutePath(); 
     final String uploadFileName = "MyJson.json"; 
    FileObj = new File(uploadFilePath+"/"+uploadFileName); 
         System.out.println("file object : "+FileObj); 
         System.out.println("My file name: "+uploadFilePath+""+uploadFileName); 
         //uploadFile(uploadFilePath + "" + uploadFileName,userid); 
         StringBuffer responseBody=new StringBuffer(); 
         Log.i("", "Ready to upload file..."); 
         HttpClient client=new DefaultHttpClient(); 
         client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); 
         Log.i("", "Set remote URL..."); 
         HttpPost post=new HttpPost("http://sripadamarketing.com/awarenessu/Data/"); 
         File file = new File(uploadFilePath+"/"+uploadFileName); 
         MultipartEntity entity=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
         ContentBody cbFile = new FileBody(file, "application/json");   
         Log.i("emotinal awareness", "Adding file(s)..."); 

         try { 
          entity.addPart("param", new StringBody("sync")); 
          entity.addPart("u", new StringBody(userid)); 
          entity.addPart("jsonf", cbFile); 
          //entity.addPart("jsonf", new FileBody((FileObj), "application/json")); 
         } catch (UnsupportedEncodingException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 

         //entity.addPart("uploadedfile2", new FileBody((FileObj), "application/zip")); 
         Log.i("emotinal awareness", "Set entity..."); 
         post.setEntity(entity); 
         BufferedReader bs=null; 
         try 
         { 
         Log.i("emotinal awareness", "Upload..."); 
          HttpEntity hEntity=client.execute(post).getEntity(); 
          bs=new BufferedReader(new InputStreamReader(hEntity.getContent())); 
          Log.i("emotinal awareness", "Response length - "+hEntity.getContentLength()); 
          String s=""; 
          while(s!=null) 
          { 
          responseBody.append(s); 
          s=bs.readLine(); 
          Log.i("emotinal awareness", "Response body - "+s); 
          } 
          bs.close(); 
         } 
         catch(IOException ioe) 
         { 
         Log.i("emotinal awareness", "Error on getting response from Server, "+ioe.toString()); 
          ioe.printStackTrace(); 
          responseBody.append("..."); 
         } 

回答

1

嘗試這些庫之一,他們都是這類任務

https://github.com/loopj/android-async-http

或者

https://github.com/koush/ion

+0

你可能婉的很不錯要小心一點;如果這些鏈接永遠不會停下來,這個答案最終會看起來很愚蠢。 –

+0

非常感謝您的意見。如果你專注於他問的問題而不是發佈的答案,那會更好。 –

+0

傢伙請幫助我 –

相關問題