我正在开发小型Android应用程序,其中我想将我的图像上传到我的服务器,使用put方法和图像作为json对象的请求体。我对此提出了很多问题,这对我很有帮助。我试过了以下方式...
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
。我做错了什么。有没有办法做到这一点。需要帮忙。谢谢。
答案 0 :(得分:0)
我试图做同样的事情也许我们可以帮助我们,这是我的代码:
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();
}
}