我已经编写了连接Internet上服务器的代码,但是实现并没有给出任何结果,也没有提供Page php数据显示
RequestQueue requestQueue;
String url = " http://nooreldeen.hostkda.com/show.php ";
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url,null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("allstudents");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject respons = jsonArray.getJSONObject(i);
String id = respons.getString("id");
String name = respons.getString("name");
String info = respons.getString("info");
textView.append(id + " - " + name + "\n" + info + "\n" + "-------------" + "\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", "ERROR");
}
}
);
requestQueue.add(jsonObjectRequest);
}
<?xml version="1.0" encoding="utf-8"?>
ANDROID MAINFAIST
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Row_itm"></activity>
</application>
添加了他的截击 编译'com.android.volley:volley:1.0.0'
答案 0 :(得分:1)
在清单文件中添加Internet权限
<uses-permission android:name="android.permission.INTERNET"/>
修改强> 你的Json回答错了。应该是这样的:
{ “allstudents”:[{ “0”: “1”, “ID” 为 “1”, “1”: “差”, “姓名”: “差”, “2”: “WWW”, “信息”: “WWW”}]}
答案 1 :(得分:1)
您的Json格式错误:
错误是:
org.json.JSONException: Value <html><body><script of type java.lang.String cannot be converted to JSONObject
使用Json Validator验证Json,例如:http://jsonlint.com/ 这显示:
Error: Parse error on line 1:
{ allstudents: [{ "0
--^
Expecting 'STRING', '}', got 'undefined'
答案 2 :(得分:0)
在清单文件中提供INTERNET权限并在同一网络上运行项目。
LIKE,连接笔记本电脑一个WiFi(A是妻子的名字),如果你在手机上运行应用程序,移动电话也应该连接到A WiFi。
如果你的朗姆酒应用程序模拟器不会影响IP地址。
答案 3 :(得分:0)
public class Screen_news extends Activity
{
----------
## JSON Through Read the Data And Image Diplay in Imageview ##
ArrayList<News_Adapter> actorsList;
String NewsHeadline;
News_ContactAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen__news);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.toolbar);
final TextView myTitleText = (TextView) findViewById(R.id.title_bar_viewname_name);
if (myTitleText != null)
{
myTitleText.setText(R.string.news);
}
final ImageView imageback = (ImageView) findViewById(R.id.image_back);
imageback.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
finish();
}
});
actorsList = new ArrayList<News_Adapter>();
new JSONAsyncTask().execute(My_Url_Class.News);
ListView listview = (ListView) findViewById(R.id.list);
adapter = new News_ContactAdapter(getApplicationContext(), R.layout.custom_news_layout, actorsList);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long id)
{
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), actorsList.get(position).getNewsHeadline(), Toast.LENGTH_LONG).show();
Intent intent=new Intent(Screen_news.this,Screen_news_full_Details.class);
intent.putExtra("NewsHeadline", actorsList.get(position).getNewsHeadline());
intent.putExtra("NewsDateTime", actorsList.get(position).getNewsDateTime());
intent.putExtra("NewsImage", actorsList.get(position).getNewsImage());
intent.putExtra("NewsShortDesc", actorsList.get(position).getNewsShortDesc());
intent.putExtra("NewsDescription", actorsList.get(position).getNewsDescription());
startActivity(intent);
}
});
}
class JSONAsyncTask extends AsyncTask<String, Void, Boolean>
{
ProgressDialog dialog;
@Override
protected void onPreExecute()
{
super.onPreExecute();
dialog = new ProgressDialog(Screen_news.this);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... urls)
{
try
{
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200)
{
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("Data");
for (int i = 0; i < jarray.length(); i++)
{
JSONObject object = jarray.getJSONObject(i);
News_Adapter actor = new News_Adapter();
actor.setNewsHeadline(object.getString("NewsHeadline"));
actor.setNewsDateTime(object.getString("NewsDateTime"));
actor.setNewsImage(object.getString("NewsImage"));
actor.setNewsShortDesc(object.getString("NewsShortDesc"));
actor.setNewsDescription(object.getString("NewsDescription"));
actorsList.add(actor);
}
return true;
}
}
catch (ParseException e1)
{
e1.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (JSONException e)
{
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result)
{
dialog.cancel();
adapter.notifyDataSetChanged();
if (result == false)
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
}
}
public class News_Adapter
{
private String NewsHeadline;
private String NewsDateTime;
private String NewsShortDesc;
private String NewsDescription;
private String NewsImage;
public News_Adapter()
{
// TODO Auto-generated constructor stub
}
public News_Adapter(String NewsHeadline, String NewsDateTime, String NewsShortDesc, String NewsDescription, String NewsImage)
{
super();
this.NewsHeadline = NewsHeadline;
this.NewsDateTime = NewsDateTime;
this.NewsShortDesc = NewsShortDesc;
this.NewsDescription = NewsDescription;
this.NewsImage = NewsImage;
}
public String getNewsHeadline()
{
return NewsHeadline;
}
public void setNewsHeadline(String newsHeadline)
{
NewsHeadline = newsHeadline;
}
public String getNewsDateTime()
{
return NewsDateTime;
}
public void setNewsDateTime(String newsDateTime)
{
NewsDateTime = newsDateTime;
}
public String getNewsShortDesc()
{
return NewsShortDesc;
}
public void setNewsShortDesc(String newsShortDesc)
{
NewsShortDesc = newsShortDesc;
}
public String getNewsDescription()
{
return NewsDescription;
}
public void setNewsDescription(String newsDescription)
{
NewsDescription = newsDescription;
}
public String getNewsImage()
{
return NewsImage;
}
public void setNewsImage(String newsImage)
{
NewsImage = newsImage;
}
}
public class News_ContactAdapter extends ArrayAdapter<News_Adapter> {
ArrayList<News_Adapter> actorList;
LayoutInflater vi;
int Resource;
ViewHolder holder;
public News_ContactAdapter(Context context, int resource, ArrayList<News_Adapter> objects) {
super(context, resource, objects);
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
actorList = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.imageview = (ImageView) v.findViewById(R.id.image);
holder.tvName = (TextView) v.findViewById(R.id.Text_Title);
holder.tvDescription = (TextView) v.findViewById(R.id.Text_Time_Date);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
//holder.imageview.setImageResource(R.drawable.about_img_us);
new DownloadImageTask(holder.imageview).execute(My_Url_Class.NEWS_IMAGE_URL + actorList.get(position).getNewsImage());
holder.tvName.setText(actorList.get(position).getNewsHeadline());
holder.tvDescription.setText(actorList.get(position).getNewsDateTime());
return v;
}
static class ViewHolder
{
public ImageView imageview;
public TextView tvName;
public TextView tvDescription;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap>
{
ImageView imageview;
public DownloadImageTask(ImageView bmImage)
{
this.imageview = bmImage;
}
protected Bitmap doInBackground(String... urls)
{
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try
{
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
}
catch (Exception e)
{
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result)
{
imageview.setImageBitmap(result);
}
}
}