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

时间:2014-04-11 11:36:03

标签: php android json image

我有四张图片要通过我的应用程序发送到服务器。这是我的代码,用于将数据发送到服务器以及四个图像,但我无法理解如何将这些图像发送到服务器:

{
    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 个答案:

答案 0 :(得分:0)

希望这会对你有所帮助:

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);

您必须使用这些库apache-mine4j-0.6.jarhttpmime-4.0.1.jar