ICS Eclipse - 无法从PHP获取信息

问题描述:

我构建了一个应用程序,该应用程序从连接到MySql服务器的php文件获取内容。它适用于所有设备(基于反馈)高达4.0 当运行我的应用程序ICS时,它会得到一个解析错误或不能显示列出的项目。为什么是这样? 这是代码,在那里我得到我的数据库的内容,使用PHP文件:ICS Eclipse - 无法从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(); 
     } 
} 

感谢

+0

你不应该上加载UI线程的数据......读到的AsyncTask ......下次你应该添加堆栈跟踪太 – Selvin 2012-02-19 21:56:48

+0

的问题是基于用户反馈,所以我猜测作者无法重现这一点和/或没有任何堆栈跟踪。 – ottel142 2012-02-19 22:15:41

Android将扔在API级别11一个NetworkOnMainThreadException(Android 3.0的)及以上。正如其名称所述,您不能在主线程上打开网络连接。

原因是“Designing for Responsiveness”。如果您的服务器不可用或用户处于弱连接状态,则您的应用程序会滞后,因为它正在等待服务器响应。

为了得到这方面的一些帮助,你可以看到这一点:http://saigeethamn.blogspot.com/2010/05/http-connection-using-threads-handlers.html

+0

我试过一切,可以你们中的一些给我一个例子或代码片段或修改代码?谢谢 – 2012-02-19 22:18:42