I want to check connection with server by receiving data from this server, here is my code:
package com.example.sanzharaubakir.fin;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import cz.msebera.android.httpclient.HttpEntity;
import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.NameValuePair;
import cz.msebera.android.httpclient.client.ClientProtocolException;
import cz.msebera.android.httpclient.client.HttpClient;
import cz.msebera.android.httpclient.client.entity.UrlEncodedFormEntity;
import cz.msebera.android.httpclient.client.methods.HttpPost;
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;
import cz.msebera.android.httpclient.message.BasicNameValuePair;
import cz.msebera.android.httpclient.util.EntityUtils;
/**
* Created by sanzharaubakir on 26.07.16.
*/
public class auth extends Activity {
EditText login;
EditText pswd;
Button ok;
MyTask task;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.authorization);
ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//task = new MyTask();
//task.execute();
new MyTask().doInBackground();
}
});
}
public class MyTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
sendData();
return null;
}
/* @Override
protected void onPreExecute() {
}
protected void onPostExecute(Void result) {
}*/
}
public void sendData()
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://144.76.29.144:8001/documents/api/login");
try {
String log = login.getText().toString();
String psw = pswd.getText().toString();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", log));
nameValuePairs.add(new BasicNameValuePair("password", psw));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
int responseCode = response.getStatusLine().getStatusCode();
String tk = EntityUtils.toString(response.getEntity());
if (responseCode == 200)
{
SharedPreferences sPref = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor e = sPref.edit();
e.putString("token", tk);
e.apply();
Intent intent = getIntent();
String info = intent.getStringExtra("d");
try {
HttpClient client = new DefaultHttpClient();
HttpPost Post = new HttpPost("http://144.76.29.144:8001");
List<NameValuePair> ValuePairs = new ArrayList<NameValuePair>(2);
ValuePairs.add(new BasicNameValuePair("data", info));
ValuePairs.add(new BasicNameValuePair("token", tk));
Post.setEntity(new UrlEncodedFormEntity(ValuePairs,"UTF-8"));
HttpResponse httpResponse = client.execute(Post);
HttpEntity httpEntity = httpResponse.getEntity();
info = EntityUtils.toString(httpEntity,"UTF-8");
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
else {
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
and XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/login"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pswd"
/>
</LinearLayout>
<Button
android:id="@+id/ok"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Log in"
/>
</LinearLayout>
When I try to click on the button the application unfortunately stops, does anyone has idea why it happens? Here is what Log says:
FATAL EXCEPTION: main
Process: com.example.sanzharaubakir.fin, PID: 2620
java.lang.NullPointerException
at com.example.sanzharaubakir.fin.auth.sendData(auth.java:76)
at com.example.sanzharaubakir.fin.auth$MyTask.doInBackground(auth.java:60)
at com.example.sanzharaubakir.fin.auth$1.onClick(auth.java:47)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)