[Java for Android App Development] Aroeris:
Some help and question (part 2)

Hello,

Sorry for the late answer, I had few issues this week :/

So nop my little application is not working, it is still crashing when i start the activity. I really don't know what I am doing wrong.

The php script return something like this : {"id":"5358","gender":"man","age":"35-44","occupation":"Executive - Managerial"}

Using at the end

echo json_encode($user);

So far i think it's good :p

I'm pasting my code if you don't mind to correct me ?

Here is the activity code :

package com.example.recmovieapp;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class UserInfoActivity extends Activity
{
TextView tID = null;
TextView tGender = null;
TextView tAge = null;
TextView tOccupation = null;

[MENTION=439709]override[/MENTION]
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tID = (TextView) findViewById(R.id.UId);
tGender = (TextView) findViewById(R.id.UGender);
tAge = (TextView) findViewById(R.id.UAge);
tOccupation = (TextView) findViewById(R.id.UOccupation);

new getUserInfo().execute();

}

[MENTION=439709]override[/MENTION]
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

public class getUserInfo extends AsyncTask<String, Void, Boolean>
{

Context mContext = null;
Exception exception = null;
User myUser;

[MENTION=439709]override[/MENTION]
protected Boolean doInBackground(String... arg0)
{

try
{
//Create the HTTP request
HttpParams httpParameters = new BasicHttpParams();

//Setup timeouts
HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
HttpConnectionParams.setSoTimeout(httpParameters, 15000);

HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost(adress);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

String result = EntityUtils.toString(entity);

// Create a JSON object from the request response
JSONObject jsonObject = new JSONObject(result);

//Retrieve the data from the JSON object
myUser = new User(jsonObject.getInt("id"),jsonObject.getString("gender"),jsonObject.getString("age"),jsonObject.getString("occupation"));

}
catch (Exception e)
{
Log.e("ClientServerDemo", "Error:", e);
exception = e;
}

return true;
}

[MENTION=439709]override[/MENTION]
protected void onPostExecute(Boolean valid)
{
//Update the UI
tID.setText("ID : " + myUser.getID());
tGender.setText("Gender : " + myUser.getGender());
tAge.setText("Age : " + myUser.getAge());
tOccupation.setText("Occupation : " + myUser.getOccupation());

if(exception != null)
{
Toast.makeText(mContext, exception.getMessage(), Toast.LENGTH_LONG).show();
}
}

}
}

My manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="schemas.android.com/apk/res/android"
package="com.example.recmovieapp"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
[B]<uses-permission android:name="android.permission.INTERNET" />[/B]
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.recmovieapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.recmovieapp.UserInfoActivity"
android:label="@string/title_activity_user_info" >
</activity>
</application>

</manifest>

And here is my logcat :

I really don't understand what's wrong :(

it seems it gives an error when it is onPostExecute, when i am setting my textviews.

PS : I had to remove all URL that was in my post as the forum requested me to do so.