从android应用程序将图像上传到服务器
问题描述:
我正在开发一个小型的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
。我做错了什么。有没有办法做到这一点。需要帮忙。谢谢。
答
我尝试做同样的我们也许可以帮助我们,这里是我的代码:
private class UploadFileToServer extends AsyncTask<Void, Void, Void> {
Bitmap image;
String name;
public UploadFileToServer(Bitmap img, String name){
this.image = img;
this.name = name;
}
@Override
protected Void doInBackground(Void... params) {
//creation of bytearray
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
guardem al byteArrayOutputStream anterior
this.image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
//codifing image
String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(),Base64.DEFAULT);
//array list creation and add what we want to send
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("image",encodedImage));
dataToSend.add(new BasicNameValuePair("name",this.name));
//i have dont understand so much from here to down....
HttpParams HttpRequestParams = getHttpRequestParams();
//send data by post server adres is my local api "ip/myapi/index.php"
HttpClient client = new DefaultHttpClient(HttpRequestParams);
HttpPost post = new HttpPost(SERVER_ADRESS+"index.php");
try{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
//i take this part from you i hope it will work not tryied yet
HttpResponse response = client.execute(post);
if (response != null) {
if (response.getStatusLine().getStatusCode() == 204)
{
//so when you are here its all right?
}
}
} catch (Exception e){
e.printStackTrace();
}
return null;
}
private HttpParams getHttpRequestParams(){
HttpParams HttpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(HttpRequestParams, 1000 * 30);
HttpConnectionParams.setSoTimeout(HttpRequestParams,1000*30);
return HttpRequestParams;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Toast.makeText(getApplicationContext(), "image uploaded", Toast.LENGTH_LONG).show();
}
}
你提供正确的链接 – 2013-03-15 06:04:01
哪个环节?我的api链接正确吗? – nilkash 2013-03-15 06:04:48
您可能缺少INTERNET权限。 – Rajesh 2013-03-15 06:05:20