开发者

android java mysql : Trouble with Data Loss when moving from Activity to Activity where each one performs an HTTP >php > mysql call

UPDATE

I am still debugging this issue and still have not found a solution. Here are some more details:

I am not using XML or JSON to retrieve data in my original implimentatin. I have a custom php script that runs from my server, and I have edited the code to parse the database query into a Java string that can easily be parsed into a String Array using the .split(","); command. ex: $return="item 1,item 2,item 3,item 4"; (see the code below)

The data is coming back accurately from the database in Act 3, which returns an array in the format I specified above, then the data only partially comes back for Act 4: the last item on the list does not return data. Then on Act 5 no data is returned and not errors are thrown. I've tried pretty much everything to debug and am at a loss.

/////////////////////////////////////////////////////////////////////////////

As the title states, I am having trouble in my app where I am navigating from one Activity to another where each one calls to a ph开发者_Go百科p, then to a database and returns information. See this diagram:

  • Activity 1: ListActivity, builds a list from local xml
  • Activity 2: ListActivity, builds a list from local xml
  • Activity 3: ListActivity, gets data from a mysql database (HttpClient to php file to db) and uses it to build a list
  • Activity 4: ListActivity, gets data from a mysql database using data from Activity 3 (HttpClient to php file to db) and uses it to build another list
  • Activity 5: Activity, gets data from a mysql database using data from Activity 4 (HttpClient to php file to db) and displays it as a TextView.

The problem is that by the time I get to Activity 5, the data does not return; I get a blank return. I am sending vars to a php script which has been web tested and indeed operates and returns the data correctly. Further, I have alerted all of my data to insure the right vars are being sent to the php files.

Without seeing 5 java docs of code, can any experts, or just anyone, perhaps tell me if I am breaking a cardinal Android / Java rule here?

Added the code for the Activities below::

private String KEY_121 = "my php file";

public class db_receive extends ListActivity {
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.db_layout);
  String results = getServerData(KEY_121);
  String[] list = results.toString().split(",");
  setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, list));
}


private String getServerData(String returnString) {
    InputStream is = null;
    String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair(ret_att1,ret_data1));
//http post
try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(KEY_121);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
}catch(Exception e){
          Log.e("log_tag", "Error in http connection "+e.toString());
    }

    //convert response to string
    try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
            }
            is.close();
            result=sb.toString();
    }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
    }
    returnString = result;
    return returnString; 
}

That is the basic layout of all 3 Activities that send queries to a php file (which grabs data from database) and receivse data, then performs a function. The last Activity is a TextView as opposed to a ListView.


There is no "cardinal rule" here. Have you debugged the Activity that fails? You can try making the Activity 5 your initial or root Activity (hardcoding the needed parameters) and seeing what happens, maybe you can get a clue on what is going on. Else, we need more info/code.


I debugged my issue. Through debugging efforts I realized that the last item on any list that came from the database had an error passing data to the next Activity. The java code was appending a carriage return on the end of the string, which was becomming a string array... Fixing that fixed my issue; works fine now.


As soon as you pull the data from the web you should be storing it in a SQLite database. Then use a CursorAdapter from to bind the data from your database to your list activities. There is a great video from Google I/O 2010 on building RESTful android apps that explains why you need to do this.

First off you are violating a cardinal rule of android development by not persisting your database immediately in permanent storage. Listen to the google engineer in the video link I posted.

Second you should not make textview the root element in your XML file. You should put the textview in a linear layout or table layout.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜