ICS Eclipse - 无法从 PHP 获取信息

发布于 2025-01-06 17:54:39 字数 4640 浏览 0 评论 0原文

我构建了一个应用程序,它从连接到 MySql 服务器的 php 文件获取内容。它适用于最高 4.0 的所有设备(基于反馈) 运行我的应用程序 ICS 时,它出现解析错误或无法显示列出的项目。这是为什么呢? 这是代码,我使用 PHP 文件从数据库获取内容:

package com.fireplace.software;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;

public class GetContentFromDBActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listofapps);

        LoadData();
    }


    void GenData(){
        ArrayList<ItemSkel> list = new ArrayList<ItemSkel>();
        for(int i = 0; i< 10; ++i) {
            list.add(new ItemSkel("id " + i, "label " + i, "path " + i, "description " + i, "ptype " + i));
        }

        Gson gson = new Gson();
        String json = gson.toJson(list);

        Toast.makeText(GetContentFromDBActivity.this, json, Toast.LENGTH_LONG).show();
    }

    public void btnLoadData(View v) {
        LoadData();
    }


    void LoadData(){

        //TextView v = (TextView)findViewById(R.id.txtStatusError); 
        try {

            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpGet httpGet = new HttpGet("http://www.u2worlds.com/fp/getdata.php");
            HttpResponse response = httpClient.execute(httpGet, localContext);
            String result = "";

            BufferedReader reader = new BufferedReader(
                new InputStreamReader(
                  response.getEntity().getContent()
                )
              );

            String line = null;
            while ((line = reader.readLine()) != null)
              result += line;

            Type type = new TypeToken<ArrayList<ItemSkel>>(){}.getType();
            Gson g = new Gson();
            final ArrayList<ItemSkel> list = g.fromJson(result, type);
            ArrayList<String> stringArray = new ArrayList<String>();
            for (ItemSkel item : list) 
                stringArray.add("" + item.getLabel());

            ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
            ListView lv = (ListView)findViewById(R.id.lwApps); 
            lv.setAdapter(modeAdapter);

            lv.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView<?> parent, View view,
                  int position, long id) {
                // When clicked, show a toast with the TextView text
                  ItemSkel currentItem = list.get(position);
                  Intent i = new Intent(getApplicationContext(), ViewInfoActivity.class);
                  i.putExtra("title", currentItem.getLabel());
                  i.putExtra("icon", currentItem.getIcon());
                  i.putExtra("link", currentItem.getPath());
                  i.putExtra("desc", currentItem.getDescription());
                  i.putExtra("ptype", currentItem.getPtype());
                  startActivity(i);
                  }
            });

            //Toast.makeText(GetContentFromDBActivity.this, "No Error", Toast.LENGTH_LONG).show();
        }
        catch (Exception ex) {
            //v.setText(ex.getMessage());
            Toast.makeText(GetContentFromDBActivity.this, "Could not connect!", Toast.LENGTH_LONG).show();
        }
    }
    public void updateProgress(int currentSize, int totalSize){
        //Toast.makeText(this, "Packages: " + Long.toString((currentSize/totalSize)*100)+"% Complete", Toast.LENGTH_SHORT).show();
        }
}

谢谢

i build an app which get's content from a php file which connects to a MySql server. It works on every device (based on feedback) up to 4.0
When running my app ICS, it gets an parse error or cant show the items listed. Why is this?
This is the code, where i get the content from my DB, using a PHP file:

package com.fireplace.software;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;

public class GetContentFromDBActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listofapps);

        LoadData();
    }


    void GenData(){
        ArrayList<ItemSkel> list = new ArrayList<ItemSkel>();
        for(int i = 0; i< 10; ++i) {
            list.add(new ItemSkel("id " + i, "label " + i, "path " + i, "description " + i, "ptype " + i));
        }

        Gson gson = new Gson();
        String json = gson.toJson(list);

        Toast.makeText(GetContentFromDBActivity.this, json, Toast.LENGTH_LONG).show();
    }

    public void btnLoadData(View v) {
        LoadData();
    }


    void LoadData(){

        //TextView v = (TextView)findViewById(R.id.txtStatusError); 
        try {

            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpGet httpGet = new HttpGet("http://www.u2worlds.com/fp/getdata.php");
            HttpResponse response = httpClient.execute(httpGet, localContext);
            String result = "";

            BufferedReader reader = new BufferedReader(
                new InputStreamReader(
                  response.getEntity().getContent()
                )
              );

            String line = null;
            while ((line = reader.readLine()) != null)
              result += line;

            Type type = new TypeToken<ArrayList<ItemSkel>>(){}.getType();
            Gson g = new Gson();
            final ArrayList<ItemSkel> list = g.fromJson(result, type);
            ArrayList<String> stringArray = new ArrayList<String>();
            for (ItemSkel item : list) 
                stringArray.add("" + item.getLabel());

            ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
            ListView lv = (ListView)findViewById(R.id.lwApps); 
            lv.setAdapter(modeAdapter);

            lv.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView<?> parent, View view,
                  int position, long id) {
                // When clicked, show a toast with the TextView text
                  ItemSkel currentItem = list.get(position);
                  Intent i = new Intent(getApplicationContext(), ViewInfoActivity.class);
                  i.putExtra("title", currentItem.getLabel());
                  i.putExtra("icon", currentItem.getIcon());
                  i.putExtra("link", currentItem.getPath());
                  i.putExtra("desc", currentItem.getDescription());
                  i.putExtra("ptype", currentItem.getPtype());
                  startActivity(i);
                  }
            });

            //Toast.makeText(GetContentFromDBActivity.this, "No Error", Toast.LENGTH_LONG).show();
        }
        catch (Exception ex) {
            //v.setText(ex.getMessage());
            Toast.makeText(GetContentFromDBActivity.this, "Could not connect!", Toast.LENGTH_LONG).show();
        }
    }
    public void updateProgress(int currentSize, int totalSize){
        //Toast.makeText(this, "Packages: " + Long.toString((currentSize/totalSize)*100)+"% Complete", Toast.LENGTH_SHORT).show();
        }
}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒解孤独 2025-01-13 17:54:39

Android 将在 API 级别 11 (Android 3.0) 及更高版本。顾名思义,您不能在主线程上打开网络连接。

这样做的原因是“响应式设计”原则。如果您的服务器不可用或用户连接较弱,您的应用程序会因为等待服务器响应而滞后。

要获得有关此问题的帮助,您可以阅读以下内容: http ://saigeethamn.blogspot.com/2010/05/http-connection-using-threads-handlers.html

Android will throw a NetworkOnMainThreadException on API Level 11 (Android 3.0) and above. As its name says, you must not open a network connection on the main thread.

The reason for this is the principle "Designing for Responsiveness". If your server is not available or the user is on a weak connection, your app lags because it's waiting for a server response.

To get some help on this, you can read this: http://saigeethamn.blogspot.com/2010/05/http-connection-using-threads-handlers.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文