dongshan2680 2014-04-11 11:36
浏览 46

如何在Android应用程序中将图像发送到服务器

I have four images which I want to send to server through my application. Here is my code for send data to sever along with the four images but I can't understand how can I send these images of mine to server:

{
    String imageUrl1 = selectedImagePath1;
    aq.id(imageview1).image(imageUrl1, true, true, 200, 0);

    String imageUrl2 = selectedImagePath2;
    aq.id(imageview2).image(imageUrl2, true, true, 200, 0);

    String imageUrl3 = selectedImagePath3;
    aq.id(imageview3).image(imageUrl3, true, true, 200, 0);

    String imageUrl4 = selectedImagePath4;
    aq.id(imageview4).image(imageUrl4 ,true, true, 200, 0);

    new GetData().execute();
}

private class GetData extends AsyncTask<String, String, String>
{
    @Override
    protected String doInBackground(String... params)
    {
        String Title = title.getText().toString();
        String Description = description.getText().toString().trim();
        String PhoneNumber = phonenumber.getText().toString();
        String Email = email.getText().toString();
        String Price = price.getText().toString();
        try
        {
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet("http://tradewale.com/open/tradedata.php?home_cat=1");
            HttpResponse response = httpclient.execute(httpget);
            BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null)
            {
                sb.append(line + NL);
            }
            in.close();
            String result = sb.toString();
            System.out.println(result);

            JSONObject obj = new JSONObject(result);
            value = obj.getString("Success");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return value;
    }
    @Override
    protected void onPostExecute(String result)
    {
        int val=Integer.parseInt(value);
        if (val==1)
        {
            Toast.makeText(PostAddActivity.this, "Thank you For Inquiry ",
                           Toast.LENGTH_LONG).show();
            finish();
        }
        else
        {
            Intent in = new Intent(getApplicationContext(), PostAddActivity.class);
            startActivity(in);
        }
    }
}
  • 写回答

1条回答 默认 最新

  • dtamho6404 2014-04-11 11:45
    关注

    Hope this will help you:

    HttpPost httpPostRequest = new HttpPost(URL);
    
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    MultipartEntity mpEntity = new MultipartEntity();
    ContentBody cbFile = new FileBody(file, "image/jpeg");
    mpEntity.addPart("file", cbFile); 
    httpPostRequest.setEntity(mpEntity);
    HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
    

    You have to use these libs apache-mine4j-0.6.jar and httpmime-4.0.1.jar for this.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?