通过HTTP POST在PHP服务器的Android LN图片上传获得http连接显示java.lang.NullPointerException错误 [英] ln image uploading through http post in php server in android get the Error in http connection java.lang.NullPointerException
问题描述
嗨余米试图通过在android系统不同的方法来上传服务器中的形象,但它错误的logcat
错误http连接显示java.lang.NullPointerException
和这里是我的code PLZ告诉我,是我在这个code图像上传
错误 BitmapFactory.Options选项=新BitmapFactory.Options(); //德code位与inSampleSize集
options.inJustDe codeBounds = TRUE;
//您的图像位于图像路径`String`
BitmapFactory.de codeFILE(selectedPath1,期权); INT bitmapWidth = 400;
INT bitmapHeight = 250;
最终诠释身高= options.outHeight;
最终诠释宽度= options.outWidth;
INT inSampleSize = 1;
如果(高度> bitmapHeight ||宽度GT; bitmapWidth){
如果(宽>高度){
inSampleSize = Math.round((浮点)高度
/(浮点)bitmapHeight);
}其他{
inSampleSize = Math.round((浮点)宽度
/(浮点)bitmapWidth);
}
} options.inJustDe codeBounds = FALSE;
options.inSampleSize = inSampleSize; 位图bmpScale = BitmapFactory.de codeFILE(selectedPath1,
选项); ByteArrayOutputStream BOS =新ByteArrayOutputStream(); //的COM pressFormat设置为JPG格式,您可以更改为PNG或
//不管你
//希望; bmpScale.com preSS(比较pressFormat.JPEG,100,BOS);
bmpScale.com preSS(比较pressFormat.JPEG,100,BOS); 字节[]数据= bos.toByteArray();
MultipartEntity实体=新MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
// entity.addPart(阿凡达,新ByteArrayBody(数据,mSignMessg +
// - +新的随机()nextInt(1000)+.JPG))。 entity.addPart(形象,新ByteArrayBody(数据,pic.jpg));
//在实体添加其他名称值对。
ArrayList的<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;(); namevaluepairs中
。新增(新BasicNameValuePair(user_ID的mSignMessg));
nameValuePairs.add(新BasicNameValuePair(名,MNAME
。.getText()的toString()));
nameValuePairs.add(新BasicNameValuePair(DOB,mbirthday
。.getText()的toString()));
nameValuePairs.add(新BasicNameValuePair(生物,mbio.getText()
的ToString()));
nameValuePairs.add(新BasicNameValuePair(性,mSexValue));
namevaluepairs中
。新增(新BasicNameValuePair(PROFILE_STATUS,0));
nameValuePairs.add(新BasicNameValuePair(位置,mlocation
。.getText()的toString())); // nameValuePairs.add(新BasicNameValuePair(形象,BA1));
Log.d(userfile的,userfile的+ BA1); // nameValuePairs.add(新BasicNameValuePair(形象,BA1)); 的for(int i = 0; I< nameValuePairs.size();我++){ 尝试{
entity.addPart(
nameValuePairs.get(ⅰ).getName(),
新StringBody(nameValuePairs.get(ⅰ).getValue()));
}赶上(UnsupportedEncodingException五){
// TODO自动生成catch块
Log.d(respons,形象respons+ E);
e.printStackTrace();
} } // httppost.setEntity(实体);
//
// Htt的presponse响应= httpClient.execute(httppost); 尝试{
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(
http://iapptechnologies.com/snapic/profileXml.php);
httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
httppost.setEntity(实体); Log.d(namevaluepairs中,namevaluepairs中+ namevaluepairs中); HTT presponse响应= httpclient.execute(httppost);
HttpEntity使用实体= response.getEntity(); //打印的反响
输出= EntityUtils.toString(response.getEntity());
Log.i(GET RESPONSE--,输出);
Log.d(GET RESPONSE--,输出); //是= entity.getContent();
Log.e(log_tag ******,良好的连接);
的System.out.println(gudconection);
Log.d(上帝连接,GUD连接); bitmapOrg.recycle(); }赶上(例外五){ Log.e(logCatch块***,
错误http连接+ e.toString());
Log.d(log_catch块******,在http连接错误
+ e.toString());
}
****************LogCat*******************
13 12-25:05:21.361:D / dalvikvm(390):GC_FOR_MALLOC释放9503对象/ 583600字节
在94ms 12-25 13:05:30.901:D /的setpath(390)的setpath到/ mnt / SD卡/图片/ Poonam.jpg 12-25 13:05:30.911:D /的setpath(390)的setpath到/ mnt / SD卡/图片/ Poonam.jpg
12-25 13:05:30.911:我/的System.out(390):selectedPath1:到/ mnt / SD卡/图片/ Poonam.jpg
12-25 13:05:37.130:D / IMAGE_NAME(390):IMAGE_NAME空
12-25 13:05:38.060:D / dalvikvm(390):GC_EXTERNAL_ALLOC释放3329对象/
在636ms 235376字节
12-25 13:06:06.541:D / GET RESPONSE - (390):&下; XML版本=1.0>
12-25 13:06:06.541:D / GET RESPONSE - (390):其中;结果>
12-25 13:06:06.541:D / GET RESPONSE - (390):<&交易GT; snapic / profileXml< /交易>12-25 13:06:06.541:D / GET RESPONSE - (390):其中;成功>真< /成功与GT;
12-25 13:06:06.541:D / GET RESPONSE - (390):其中;消息>成功简介
更新< /信息>
12-25 13:06:06.541:D / GET RESPONSE - (390):其中,/结果>
1
12-25 13:06:06.551:E / logCatch块***(390):在http连接错误
显示java.lang.NullPointerException12-25 13:06:06.551:D / log_catch块******(390):错误http连接显示java.lang.NullPointerException
在看到您是从网络获得成功的价值,但只有你得到NullPointerException异常后,打印响应后您正在做两件事logcat的
1.印刷一些日志和的System.out.println
2.回收位图
所以,请检查bitmapOrg值为null或不是之前回收。
Hi I m try to upload the image in server through different method in android but it error in logcat Error in http connection java.lang.NullPointerException
and here is my code plz tell me where is my mistake in this code for image uploading
BitmapFactory.Options options = new BitmapFactory.Options();
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = true;
// image path `String` where your image is located
BitmapFactory.decodeFile(selectedPath1, options);
int bitmapWidth = 400;
int bitmapHeight = 250;
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > bitmapHeight || width > bitmapWidth) {
if (width > height) {
inSampleSize = Math.round((float) height
/ (float) bitmapHeight);
} else {
inSampleSize = Math.round((float) width
/ (float) bitmapWidth);
}
}
options.inJustDecodeBounds = false;
options.inSampleSize = inSampleSize;
Bitmap bmpScale = BitmapFactory.decodeFile(selectedPath1,
options);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// CompressFormat set up to JPG, you can change to PNG or
// whatever you
// want;
bmpScale.compress(CompressFormat.JPEG, 100, bos);
bmpScale.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
// entity.addPart("avatar", new ByteArrayBody(data,mSignMessg +
// "-" + new Random().nextInt(1000) + ".jpg"));
entity.addPart("image", new ByteArrayBody(data, "pic.jpg"));
// add your other name value pairs in entity.
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs
.add(new BasicNameValuePair("user_id", mSignMessg));
nameValuePairs.add(new BasicNameValuePair("name", mname
.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("dob", mbirthday
.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("bio", mbio.getText()
.toString()));
nameValuePairs.add(new BasicNameValuePair("sex", mSexValue));
nameValuePairs
.add(new BasicNameValuePair("profile_status", "0"));
nameValuePairs.add(new BasicNameValuePair("location", mlocation
.getText().toString()));
// nameValuePairs.add(new BasicNameValuePair("image", ba1));
Log.d("userfile", "userfile " + ba1);
// nameValuePairs.add(new BasicNameValuePair("image", ba1));
for (int i = 0; i < nameValuePairs.size(); i++) {
try {
entity.addPart(
nameValuePairs.get(i).getName(),
new StringBody(nameValuePairs.get(i).getValue()));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
Log.d("respons", "image respons " + e);
e.printStackTrace();
}
}
// httppost.setEntity(entity);
//
// HttpResponse response = httpClient.execute(httppost);
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://iapptechnologies.com/snapic/profileXml.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httppost.setEntity(entity);
Log.d("nameValuePairs", "nameValuePairs " + nameValuePairs);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity1 = response.getEntity();
// print responce
outPut = EntityUtils.toString(response.getEntity());
Log.i("GET RESPONSE—-", outPut);
Log.d("GET RESPONSE—-", outPut);
// is = entity.getContent();
Log.e("log_tag ******", "good connection");
System.out.println("gudconection");
Log.d("god connection ", "gud connection ");
bitmapOrg.recycle();
} catch (Exception e) {
Log.e("logCatch block***",
"Error in http connection " + e.toString());
Log.d("log_catch block ******", "Error in http connection "
+ e.toString());
}
****************LogCat*******************
12-25 13:05:21.361: D/dalvikvm(390): GC_FOR_MALLOC freed 9503 objects / 583600 bytes
in 94ms
12-25 13:05:30.901: D/setpath(390): setpath /mnt/sdcard/Picture/ Poonam.jpg
12-25 13:05:30.911: D/setpath(390): setpath /mnt/sdcard/Picture/ Poonam.jpg
12-25 13:05:30.911: I/System.out(390): selectedPath1 : /mnt/sdcard/Picture/ Poonam.jpg
12-25 13:05:37.130: D/image_name(390): image_name null
12-25 13:05:38.060: D/dalvikvm(390): GC_EXTERNAL_ALLOC freed 3329 objects /
235376 bytes in 636ms
12-25 13:06:06.541: D/GET RESPONSE—-(390): <?xml version="1.0"?>
12-25 13:06:06.541: D/GET RESPONSE—-(390): <Result>
12-25 13:06:06.541: D/GET RESPONSE—-(390):
<Transaction>snapic/profileXml</Transaction>
12-25 13:06:06.541: D/GET RESPONSE—-(390): <Success>True</Success>
12-25 13:06:06.541: D/GET RESPONSE—-(390): <Message>Profile successfully
updated</Message>
12-25 13:06:06.541: D/GET RESPONSE—-(390): </Result>
1
12-25 13:06:06.551: E/logCatch block***(390): Error in http connection
java.lang.NullPointerException
12-25 13:06:06.551: D/log_catch block ******(390): Error in http connection java.lang.NullPointerException
While seeing the Logcat you are getting success value from Network but after that only you are getting NullPointerException, After printing the response you are doing two thing 1. Printing Some Log and system.out.println 2. Recycle the bitmap
So Please check the bitmapOrg value is null or not before recycle it.
这篇关于通过HTTP POST在PHP服务器的Android LN图片上传获得http连接显示java.lang.NullPointerException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!