嘿伙计们,我在将我的发送图像下载到数据库时遇到问题。
解码解码器=返回假。这是来自 logcat 的错误
这是代码;解析 JSON 数据。我认为这里有一些问题。我的程序应该将照片从 android 发送到数据库。我使用Base64进行解码。在其他活动中,我可以通过解析数据下载这张照片。
public class ParsingData extends AsyncTask<String, Void, JSONObject>{
ProgressDialog progressDialog ;
GetJSONListener getJSONListener;
Context curContext;
public ParsingData(Context context, GetJSONListener listener){
this.getJSONListener = listener;
curContext = context;
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public static JSONObject connect(String url)
{
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response;
try {
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String result= convertStreamToString(instream);
JSONObject json=new JSONObject(result);
instream.close();
return json;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
public void onPreExecute() {
progressDialog = new ProgressDialog(curContext);
progressDialog.setMessage("Loading..Please wait..");
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.show();
}
String url ="http://jijio.host22.com/json.php";
ParsingData client = new ParsingData(this, l);
client.execute(url);
}
GetJSONListener l = new GetJSONListener(){
@Override
public void onRemoteCallComplete(JSONObject jsonFromNet) {
// add code to act on the JSON object that is returned
try {
JSONArray contacts = jsonFromNet.getJSONArray("recipes");
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString("id");
try {
if(i==1){
String photo = c.getString("image");
System.out.println(c.getString("image"));
byte[] encoded = Base64.decode(photo);
setImageViewWithByteArray(image, encoded);
System.out.print(encoded);}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(id);}
System.out.print( jsonFromNet.getString("id"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};