รายละเอียดของการตอบ ::
ขอบคุณสำหรับคำแนะนำค่ะ
ได้เคยลองดึงข้อมูลที่เป็น Text โดย Get By ID มาแล้วค่ะ สามารถดึงมาแสดงได้
และได้ลองปรับมาแก้ไขกับรูปภาพแล้ว แต่ก็ยังดึงมาไม่ได้ค่ะ
Code (Android-Java)
private void showImage() {
final TextView tid = (TextView) findViewById(R.id.txtid);
final ImageView imageView = (ImageView) findViewById(R.id.imgHouse);
String url1 = "http://www.zp9512.tld.122.155.18.18.no-domain.name/RegTest/getImageById.php";
Intent intent = getIntent();
final String id = intent.getStringExtra("id");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("sid", id));
String resultServer = getHttpGet(url1, params);
String strid = "";
String strImage = "";
JSONObject c1;
try {
c1 = new JSONObject(resultServer);
strid = c1.getString("id");
if (!strid.equals("")) {
tid.setText(strid);
imageView.setImageBitmap(loadBitmap(url1, params));
} else {
tid.setText("-");
imageView.setImageResource(android.R.drawable.ic_menu_report_image);
Toast.makeText(DetailActivity.this, "Load Image Failed", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private String getHttpGet(String url1, List<NameValuePair> params) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url1);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download file..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
private static final String TAG = "ERROR";
private static final int IO_BUFFER_SIZE = 4 * 1024;
public static Bitmap loadBitmap(String url1, List<NameValuePair> params) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url1).openStream(), IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//option.inSampleSize =1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url1);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
private static void closeStream(Closeable stream) {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
android.util.Log.e(TAG, "Could not close stream" , e);
}
}
}
private static void copy(InputStream in, OutputStream out) throws IOException {
byte[] b = new byte[IO_BUFFER_SIZE];
int read;
while ((read = in.read(b)) != -1) {
out.write(b,0,read);
}
}
แล้วดูจาก Android Monitor แล้วมีปัญหา ดังนี้ค่ะ พอดีแล้วมันเป็น บรรทัดนี้ค่ะ c1 = new JSONObject(resultServer);
03-07 12:08:03.914 6235-6235/? W/System.err: org.json.JSONException: End of input at character 0 of
03-07 12:08:03.914 6235-6235/? W/System.err: at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
03-07 12:08:03.914 6235-6235/? W/System.err: at org.json.JSONTokener.nextValue(JSONTokener.java:97)
03-07 12:08:03.914 6235-6235/? W/System.err: at org.json.JSONObject.<init>(JSONObject.java:155)
03-07 12:08:03.914 6235-6235/? W/System.err: at org.json.JSONObject.<init>(JSONObject.java:172)
03-07 12:08:03.914 6235-6235/? W/System.err: at training.ppproject.app.getmember.DetailActivity.showImage(DetailActivity.java:111)
03-07 12:08:03.914 6235-6235/? W/System.err: at training.ppproject.app.getmember.DetailActivity.onCreate(DetailActivity.java:66)
03-07 12:08:03.914 6235-6235/? W/System.err: at android.app.Activity.performCreate(Activity.java:5231)
03-07 12:08:03.914 6235-6235/? W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-07 12:08:03.918 6235-6235/? W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
03-07 12:08:03.918 6235-6235/? W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
03-07 12:08:03.918 6235-6235/? W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-07 12:08:03.918 6235-6235/? W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-07 12:08:03.918 6235-6235/? W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
03-07 12:08:03.918 6235-6235/? W/System.err: at android.os.Looper.loop(Looper.java:136)
03-07 12:08:03.918 6235-6235/? W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5001)
03-07 12:08:03.918 6235-6235/? W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
03-07 12:08:03.918 6235-6235/? W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
03-07 12:08:03.918 6235-6235/? W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
03-07 12:08:03.918 6235-6235/? W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
03-07 12:08:03.918 6235-6235/? W/System.err: at dalvik.system.NativeStart.main(Native Method)
รบกวนช่วยตรวจสอบทีค่ะ