【问题标题】:How to send images to server in android application如何在android应用程序中将图像发送到服务器
【发布时间】:2014-04-12 00:10:28
【问题描述】:

我有四个图像,我想通过我的应用程序发送到服务器。这是我将数据与四个图像一起发送到服务器的代码,但我不明白如何将我的这些图像发送到服务器:

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

【问题讨论】:

标签: php android json image


【解决方案1】:

希望对您有所帮助:

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

【讨论】:

  • Rajesh 我收到此错误套接字失败:EACCES(权限被拒绝)
  • 如果您在模拟器中测试 m 192.168.1.9/openeans 尝试使用主机 ip(服务器 ip)
  • 或检查是否提供了互联网权限
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-06
  • 2015-10-03
  • 1970-01-01
相关资源
最近更新 更多