我正在開發一個小型的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