我正在开发一个小型的android应用程序,我想通过put方法和图像将图像上传到我的服务器,并将其作为请求的主体与json object.I提供了很多关于这个和那个帮助的问题我很多。我试图用下面的方式...从android应用程序将图像上传到服务器
class ImageUploadTask extends AsyncTask <Void, Void, Void>{
@Override
protected Void doInBackground(Void... unused) {
HttpClient hc = new DefaultHttpClient();
String message;
HttpPut p = new HttpPut("https://abc.com");
JSONObject Jobject = new JSONObject();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
Jobject.put("Image", data);
try {
message = Jobject.toString();
p.setEntity(new StringEntity(message, "UTF8"));
p.setHeader("Content-type", "application/json");
HttpResponse resp = hc.execute(p);
if (resp != null) {
if (resp.getStatusLine().getStatusCode() == 204)
{
}
}
Log.d("Status line", "" + resp.getStatusLine().getStatusCode());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Void... unsued) {
}
@Override
protected void onPostExecute(Void result) {
try {
if (dialog.isShowing())
dialog.dismiss();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),"Error"+e,
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
}
但它不工作。当我试图执行我的网络调用时,它显示我的系统错误 java.net.UnknownHostException: abc.com
。我做错了什么。有没有办法做到这一点。需要帮忙。谢谢。
你提供正确的链接 – 2013-03-15 06:04:01
哪个环节?我的api链接正确吗? – nilkash 2013-03-15 06:04:48
您可能缺少INTERNET权限。 – Rajesh 2013-03-15 06:05:20