Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android使用httpclient将图像文件发布到php脚本_Java_Php_Android_Post_Apache Httpclient 4.x - Fatal编程技术网

Java android使用httpclient将图像文件发布到php脚本

Java android使用httpclient将图像文件发布到php脚本,java,php,android,post,apache-httpclient-4.x,Java,Php,Android,Post,Apache Httpclient 4.x,我试图将图像文件(PNG)发布到php脚本中,以便从$u文件中获取它们,但是,当我尝试提交数据以上载时,我的android应用程序在那里停留了很长时间,并且从系统收到一条没有响应的消息。是文件上传时间太长还是我做错了什么 public void postData() { // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost h

我试图将图像文件(PNG)发布到php脚本中,以便从$u文件中获取它们,但是,当我尝试提交数据以上载时,我的android应用程序在那里停留了很长时间,并且从系统收到一条没有响应的消息。是文件上传时间太长还是我做错了什么

public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("PHP_SCRIPT_URL");

    SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
    String term = sharedPref.getString(getString(R.string.current_term), "000");
    String location = sharedPref.getString(getString(R.string.current_location), "000");

    EditText text1 = (EditText) this.findViewById(R.id.entry_title);
    EditText text2 = (EditText) this.findViewById(R.id.entry_comments);
    String title = text1.getText().toString();
    String comment = text2.getText().toString();

    MultipartEntity multi = new MultipartEntity();

    try {
        // Add your data
        multi.addPart("term", new StringBody(term));
        multi.addPart("space", new StringBody(location));
        multi.addPart("title", new StringBody(title));
        multi.addPart("comment", new StringBody(comment));
        File cacheDir = new File(this.getCacheDir(), "thumbnails");
        if (cacheDir.exists()) {
            File cacheFile;
            for (int i = 0; i < 6; i++) {
                cacheFile = new File(cacheDir, "photo_" + i);
                if (cacheFile.exists()) {
                    multi.addPart("media"+(i+1), new InputStreamBody(new FileInputStream(cacheFile), "photo_"+i));
                }
            }
        }
        httppost.setEntity(multi);

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        int responseCode = response.getStatusLine().getStatusCode();
        switch(responseCode) {
        case 200:
            check_success = true;
            break;
        default:
            check_success = false;
            new AlertDialog.Builder(this).setTitle("ERROR").setMessage("HTTP RESPONSE: "+responseCode).setNeutralButton("Close", null).show();
        }

    } catch (ClientProtocolException e) {
        new AlertDialog.Builder(this).setTitle("ERROR").setMessage("ClientProtocolException\n" + e.getMessage()).setNeutralButton("Close", null).show();
    } catch (IOException e) {
        new AlertDialog.Builder(this).setTitle("ERROR").setMessage("IOException\n" + e.getMessage()).setNeutralButton("Close", null).show();
    }
}
public void postData(){
//创建一个新的HttpClient和Post头
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“PHP_脚本_URL”);
SharedPreferences sharedPref=this.getPreferences(Context.MODE\u PRIVATE);
String term=sharedPref.getString(getString(R.String.current_term),“000”);
字符串位置=sharedPref.getString(getString(R.String.current_位置),“000”);
EditText text1=(EditText)this.findViewById(R.id.entry\u title);
EditText text2=(EditText)this.findViewById(R.id.entry\u comments);
字符串title=text1.getText().toString();
String comment=text2.getText().toString();
MultipartEntity multi=新的MultipartEntity();
试一试{
//添加您的数据
multi.addPart(“术语”,新的StringBody(术语));
multi.addPart(“空间”,新的StringBody(位置));
multi.addPart(“标题”,新的StringBody(标题));
multi.addPart(“注释”,新的StringBody(注释));
File cacheDir=新文件(this.getCacheDir(),“缩略图”);
if(cacheDir.exists()){
文件缓存文件;
对于(int i=0;i<6;i++){
cacheFile=新文件(cacheDir,“photo_”+i);
if(cacheFile.exists()){
multi.addPart(“媒体”+(i+1),新的InputStreamBody(新文件InputStream(缓存文件),“照片”+i));
}
}
}
httppost.setEntity(多实体);
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
int responseCode=response.getStatusLine().getStatusCode();
开关(响应代码){
案例200:
检查_success=true;
打破
违约:
检查_success=false;
新建AlertDialog.Builder(this).setTitle(“错误”).setMessage(“HTTP响应:+responseCode”).setNeutralButton(“关闭”,null).show();
}
}捕获(客户端协议例外e){
新建AlertDialog.Builder(this).setTitle(“ERROR”).setMessage(“ClientProtocolException\n”+e.getMessage()).setNeutralButton(“Close”,null).show();
}捕获(IOE异常){
新建AlertDialog.Builder(this).setTitle(“错误”).setMessage(“IOException\n”+e.getMessage()).setNeutralButton(“Close”,null).show();
}
}

我建议改为这样做(使用HttpPost),它简单一点:谢谢!这帮助我走上了正确的道路。我现在只需要弄清楚如何上传我的图像文件。。。我更新了问题/代码