Monday, 29 December 2014

Android http response doesn't work



Hello I am trying to create an activity, in which the android sends values to the php script, which inserts values to a mysql database and then the response comes in an android textview. When I run the app and click the button, the dialog box appears saying "creating chatroom.." and when the process finishes the dialog box disappears and I don't get any response in the textview, but I find that the values are successfully inserted in the database. what's wrong with my code?


note:I don't get any errors, and the app doesn't crash, and I added the internet permission in the manifest.


This is the java code:



public class SubmitRoomActivity extends Activity {
String username;
String ccolor;
String crname;
Button b;
EditText et,pass;
TextView tv;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response2;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
public final static String CHATTER_NAME = "com.gowemto.gowemto.USERNAME";
public final static String CHATTER_COLOR = "com.gowemto.gowemto.CCOLOR";
public final static String CHATROOM_NAME = "com.gowemto.gowemto.CRNAME";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_submit_room);

b = (Button)findViewById(R.id.subroom);

tv = (TextView)findViewById(R.id.logerr2);

Intent intent = getIntent();
username = intent.getStringExtra(SelectRoomActivity.CHATTER_NAME);
ccolor = intent.getStringExtra(SelectRoomActivity.CHATTER_COLOR);
crname = intent.getStringExtra(SelectRoomActivity.CHATROOM_NAME);
final TextView welcominguser = (TextView) findViewById(R.id.hello_user_2);
if(ccolor.trim().equals("Black")){

welcominguser.setTextColor(Color.parseColor("#000000"));
}
else if(ccolor.trim().equals("Blue")){

welcominguser.setTextColor(Color.parseColor("#0000FF"));
}
else if(ccolor.trim().equals("Red")){

welcominguser.setTextColor(Color.parseColor("#FF0000"));
}
else if(ccolor.trim().equals("Orange")){

welcominguser.setTextColor(Color.parseColor("#FF8000"));
}
else if(ccolor.trim().equals("Green")){

welcominguser.setTextColor(Color.parseColor("#008000"));
}
else if(ccolor.trim().equals("Gray")){

welcominguser.setTextColor(Color.parseColor("#808080"));
}
else if(ccolor.trim().equals("Brown")){

welcominguser.setTextColor(Color.parseColor("#804000"));
}
else if(ccolor.trim().equals("Purple")){

welcominguser.setTextColor(Color.parseColor("#800080"));
}
else if(ccolor.trim().equals("Pink")){

welcominguser.setTextColor(Color.parseColor("#FF4080"));
}
else {

welcominguser.setTextColor(Color.parseColor("#000000"));
}
welcominguser.setText("Hello, " + username);



b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog = ProgressDialog.show(SubmitRoomActivity.this, "",
"Creating room..", true);
new Thread(new Runnable() {
public void run() {
login();
}
}).start();
}
});
}

void login(){
try{
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://10.0.2.2/mychat/createroom"); // make sure the url is correct.
//add your data
nameValuePairs = new ArrayList<NameValuePair>(3);
// Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
nameValuePairs.add(new BasicNameValuePair("username",username)); // $Edittext_value = $_POST['Edittext_value'];
nameValuePairs.add(new BasicNameValuePair("ccolor",ccolor)); // $Edittext_value = $_POST['Edittext_value'];
nameValuePairs.add(new BasicNameValuePair("roomname",crname));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP Post Request
response2=httpclient.execute(httppost);
// edited by James from coderzheaven.. from here....
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response2 = httpclient.execute(httppost, responseHandler);
runOnUiThread(new Runnable() {
public void run() {
tv.setText(response2);
dialog.dismiss();
}
});



}catch(Exception e){
dialog.dismiss();
tv.setText("Exception : " + e.getMessage());
}
}
}


and this is the xml code:



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#EED8FF">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="10dp"
>
<TextView
android:id="@+id/hello_user_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#000000"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="56dp"
android:text="@+string/room_inst_2"
android:textSize="16sp"
android:textColor="#000000"/>
<Spinner
android:id="@+id/second_colors"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/sec_color_names" />
<Button
android:id="@+id/subroom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_margin="4dp"
android:text="@string/cont_act"
style="@style/DefaultButtonText"
android:background="@drawable/button_default_bg"
android:onClick="gotoRoom"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="56dp"
android:id="@+id/logerr2"
android:textColor="#000000"/>
</LinearLayout

</RelativeLayout>


and this is the php script:



<?php
$hostname_localhost ="localhost";
$database_localhost ="chat_db";
$username_localhost ="root";
$password_localhost ="";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);

mysql_select_db($database_localhost, $localhost);

$username = $_POST['username'];
$roomname = $_POST['roomname'];
$roomcolor = $_POST['ccolor'];
$query_insert = "INSERT INTO chatrooms VALUES('$username','$roomname','$roomcolor')";
$query_exec = mysql_query($query_insert) or die(mysql_error());

echo "Room created successfully";

?>


How can I fix this problem? what's wrong with my code?


No comments:

Post a Comment