package com.example.jsontoservernetproject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import com.example.jsontoservernetproject.Person;
public class MainActivity extends Activity implements OnClickListener {
TextView tvIsConnected;
EditText etName,etCountry,etTwitter;
Button btnPost;
Person person;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvIsConnected = (TextView) findViewById(R.id.tvIsConnected);
etName = (EditText) findViewById(R.id.etName);
etCountry = (EditText) findViewById(R.id.etCountry);
etTwitter = (EditText) findViewById(R.id.etTwitter);
btnPost = (Button) findViewById(R.id.btnPost);
if(isConnected()){
tvIsConnected.setBackgroundColor(0xFF00CC00);
tvIsConnected.setText("You are conncted");
}
else{
tvIsConnected.setText("You are NOT conncted");
}
btnPost.setOnClickListener(this);
}
public static String POST(String url, Person person){
InputStream inputStream = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
String json = "";
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("name", person.getName());
jsonObject.accumulate("country", person.getCountry());
jsonObject.accumulate("twitter", person.getTwitter());
json = jsonObject.toString();
Toast.makeText(null, json,Toast.LENGTH_SHORT ).show();
StringEntity se = new StringEntity(json);
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpclient.execute(httpPost);
inputStream = httpResponse.getEntity().getContent();
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.btnPost:
if(!validate())
Toast.makeText(getBaseContext(), "Enter some data!", Toast.LENGTH_LONG).show();
new HttpAsyncTask().execute("http://localhost:8080/JsonServer/mainPage");
break;
}
}
public boolean isConnected(){
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
return true;
else
return false;
}
private class HttpAsyncTask extends AsyncTask<string,> {
@Override
protected String doInBackground(String... urls) {
person = new Person();
person.setName(etName.getText().toString());
person.setCountry(etCountry.getText().toString());
person.setTwitter(etTwitter.getText().toString());
return POST(urls[0],person);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getBaseContext(), "Data Sent!", Toast.LENGTH_LONG).show();
}
}
private boolean validate(){
if(etName.getText().toString().trim().equals(""))
return false;
else if(etCountry.getText().toString().trim().equals(""))
return false;
else if(etTwitter.getText().toString().trim().equals(""))
return false;
else
return true;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
}
XML:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<textview>
android:id="@+id/tvIsConnected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:removed="#FF0000"
android:textColor="#FFF"
android:textSize="18dp"
android:layout_marginBottom="5dp"
android:text="is connected?" />
<relativelayout>
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<textview>
android:id="@+id/tvName"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="Name"
android:layout_alignBaseline="@+id/etName"/>
<edittext>
android:id="@+id/etName"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tvName"/>
<textview>
android:id="@+id/tvCountry"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_below="@+id/tvName"
android:text="Country"
android:layout_alignBaseline="@+id/etCountry"/>
<edittext>
android:id="@+id/etCountry"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tvCountry"
android:layout_below="@+id/etName"/>
<textview>
android:id="@+id/tvTwitter"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_below="@+id/tvCountry"
android:text="Twitter"
android:layout_alignBaseline="@+id/etTwitter"/>
<edittext>
android:id="@+id/etTwitter"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tvTwitter"
android:layout_below="@+id/etCountry"/>
</edittext></textview></edittext></textview></edittext></textview></relativelayout>
<Button
android:id="@+id/btnPost"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="POST"/>
</textview></linearlayout>
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
package="com.example.jsontoservernetproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk>
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application>
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity>
android:name="com.example.jsontoservernetproject.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>
</application>
</uses-sdk></manifest>
What I have tried:
I'm beginner to android. I have tried a Android program which sends values to servlet, In the servlet i have created database(Oracle 11g) to store received data. but unfortunately it does not receive any values, just displaying null in database table. please help me out to solve this problem.