2014-02-25 89 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

家伙请帮助我 –

相关问题