【问题标题】:Upload Image - Post request to php script - Android上传图片 - 将请求发布到 php 脚本 - Android
【发布时间】:2013-07-25 02:03:39
【问题描述】:

我想通过将图像发送到 php 脚本来上传图像。如果互联网连接良好,它工作正常。但有时它不起作用。例如。当互联网连接速度很慢时。 因此,我正在寻找一种以慢速连接上传图像的方法。 我能做些什么?文件大小已经很小了,大约 40kb。 代码应该尝试上传图片,直到完成。

编辑:上传代码:

public void upload(String uploadImage){
     SharedPreferences settings = getSharedPreferences("App", 0);
     String emailaddress = settings.getString("mailaddress", null);
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
     nameValuePairs.add(new BasicNameValuePair("image",uploadImage));
     nameValuePairs.add(new BasicNameValuePair("email", emailaddress));
     try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new
            HttpPost("http://domain.de/uploadImage.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
     }catch(Exception e){
            try {
            Thread.sleep( 10000 );
            }catch (InterruptedException e1) {
            e1.printStackTrace();
            }
            upload(uploadImage);
     }
}

【问题讨论】:

  • 请分享您的代码...
  • 循环(即 2 0r 3 次)直到您从服务器收到成功。
  • 我通过捕获异常进行了尝试。但我不认为这是处理它的最佳方式。将添加代码。
  • 不,为什么要捕获异常。如果它的异常,那么它的一些其他问题

标签: php android post upload request


【解决方案1】:

使用 AsyncTask 在后台运行您的上传过程。您可以在加载操作时显示进度条。

public class UploadProgress extends AsyncTask<Void, Void, Void>
{  
@Override
protected void onPreExecute()
{  
super.onPreExecute();
dialog.setMessage("Please wait...");
dialog.setCancelable(false);
dialog.show(); 
} 
@Override 
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
// Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
}

@Override 
protected void onPostExecute(Void result) 
{ 
  //Post Execute
   dialog.cancel();
}
@Override
protected Void doInBackground(Void... params) {

// Your operation..Dont Edit Any Views here
boolean isDataSubmitted = false;
        while (!isDataSubmitted) {
            count++;
            if (count < 2)//Count depends on you {
                try {
                   //Call your upload  method here 
                   //If its success your dataSubmitted will go true

                   isDataSubmitted = true;
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }

return null;
}

【讨论】:

  • 没有别的办法了吗? Whatsapp 之类的应用似乎一直在尝试发送数据。
  • 很遗憾没有改善。可能是超时太低了?
  • 我发现有时会收到 HTTP 代码 500,这意味着“内部服务器”错误。有没有办法解决它?我正在为网站使用普通的网络空间。
  • 它说他们是 Web 服务器错误。看看here。它会给你一个想法。
猜你喜欢
  • 1970-01-01
  • 2013-07-22
  • 2014-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多