Android ดึงข้อมูล Json จาก Web server มาใส่ใน app ไม่ขึ้น แต่เรียกดูผ่าน Web browser ขึ้น

แลกเปลี่ยนประสบการณ์และ ปัญหาการใช้งาน Mobile การพัฒนา App บน SmartMobile , tablet Android, iOS, Window Phone และ ฯลฯ

Moderator: mindphp, ผู้ดูแลกระดาน

aod563121

Android ดึงข้อมูล Json จาก Web server มาใส่ใน app ไม่ขึ้น แต่เรียกดูผ่าน Web browser ขึ้น

โพสต์โดย aod563121 » 19/02/2018 12:02 pm

Web server ที่ใช้ http://tarvel.rf.gd/json.php

เปิดดูบน browser ข้อมูล Json ก็ขึ้น
พอไปไปรันบน app listview ไม่มีข้อมูล ปรากฎ ทั้งใน AVD และ โทรศัพท์

ไม่ทราบว่าเป็นที่อะไร ครับ

โค้ด: เลือกทั้งหมด

package com.example.travelguide.travelguide;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.telecom.Call;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Main10Activity extends AppCompatActivity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main10);

// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}

// listView1
final ListView lstView1 = (ListView)findViewById(R.id.list_item);





String url = "http://tarvel.rf.gd/json.php";

try {
JSONArray data = new JSONArray(getJSONUrl(url));

final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;

for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();
map.put("Name", c.getString("Name"));
map.put("Details", c.getString("Details"));
map.put("Pic1", c.getString("Pic1"));
MyArrList.add(map);
}


lstView1.setAdapter(new ImageAdapter(this,MyArrList));

final AlertDialog.Builder imageDialog = new AlertDialog.Builder(this);
final LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);






} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


public class ImageAdapter extends BaseAdapter
{
private Context context;
private ArrayList<HashMap<String, String>> MyArr = new ArrayList<HashMap<String, String>>();

public ImageAdapter(Context c, ArrayList<HashMap<String, String>> list)
{
// TODO Auto-generated method stub
context = c;
MyArr = list;
}

public int getCount() {
// TODO Auto-generated method stub
return MyArr.size();
}

public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
}

// ColImage
ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
imageView.getLayoutParams().height = 500;
imageView.getLayoutParams().width = 400;
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
try
{
imageView.setImageBitmap(loadBitmap(MyArr.get(position).get("Pic1")));
} catch (Exception e) {
// When Error
imageView.setImageResource(android.R.drawable.ic_menu_report_image);
}

// ColPosition
TextView txtPosition = (TextView) convertView.findViewById(R.id.title);
txtPosition.setPadding(10, 0, 0, 0);
txtPosition.setText( MyArr.get(position).get("Name"));

// ColPicname
TextView txtPicName = (TextView) convertView.findViewById(R.id.desc);
txtPicName.setPadding(50, 0, 0, 0);
txtPicName.setText( MyArr.get(position).get("Details"));



final String Name=MyArr.get(position).get("Name");
final String Details=MyArr.get(position).get("Details");
final String Pic1=MyArr.get(position).get("Pic1");

convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//OPEN DETAIL ACTIVITY
openDetailActivity(Name,Details,Pic1);

}
});

return convertView;

}

}

/*** Get JSON Code from URL ***/
public String getJSONUrl(String url) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Download OK
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();
}


/***** Get Image Resource from URL (Start) *****/
private static final String TAG = "ERROR";
private static final int IO_BUFFER_SIZE = 4 * 1024;
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;

try {
in = new BufferedInputStream(new URL(url).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();
//options.inSampleSize = 1;

bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url);
} 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);
}
}



private void openDetailActivity(String...details)
{
Intent i=new Intent(this,MainShowActivity.class);
i.putExtra("locationnew",details[0]);
i.putExtra("desc",details[1]);
i.putExtra("USERNAME_KEY",details[2]);

this.startActivity(i);

}




}

  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ย้อนกลับไปยัง

ผู้ใช้งานขณะนี้

กำลังดูบอร์ดนี้: 3 และ บุคคลทั่วไป 0 ท่าน