i was write code for login activity with php path server and now i just copy the code and use it in new application this is my code:-
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.apache.http.params.HttpCoectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
protected EditText useame;
private EditText password;
protected String enteredUseame;
private final String serverUrl = "http://192.168.215.14/login/index.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
useame = (EditText)findViewById(R.id.useame_field);
password = (EditText)findViewById(R.id.password_field);
Button loginButton = (Button)findViewById(R.id.login);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
enteredUseame = useame.getText().toString();
String enteredPassword = password.getText().toString();
if(enteredUseame.equals("") || enteredPassword.equals("")){
Toast.makeText(MainActivity.this, "Useame or password must be filled", Toast.LENGTH_LONG).show();
retu;
}
if(enteredUseame.length() <= 1 || enteredPassword.length() <= 1){
Toast.makeText(MainActivity.this, "Useame or password length must be greater than one", Toast.LENGTH_LONG).show();
retu;
}
// request authentication with remote server4
AsyncDataClass asyncRequestObject = new AsyncDataClass();
asyncRequestObject.execute(serverUrl, enteredUseame, enteredPassword);
}
});
;
}
private class AsyncDataClass extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HttpParams httpParameters = new BasicHttpParams();
HttpCoectionParams.setCoectionTimeout(httpParameters, 5000);
HttpCoectionParams.setSoTimeout(httpParameters, 5000);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost(params[0]);
String jsonResult = "";
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("useame", params[1]));
nameValuePairs.add(new BasicNameValuePair("password", params[2]));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
retu jsonResult;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
System.out.println("Resulted Value: " + result);
if(result.equals("") || result == null){
Toast.makeText(MainActivity.this, "Server coection failed", Toast.LENGTH_LONG).show();
retu;
}
int jsonResult = retuParsedJsonObject(result);
if(jsonResult == 0){
Toast.makeText(MainActivity.this, "Invalid useame or password", Toast.LENGTH_LONG).show();
retu;
}
if(jsonResult == 1){
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
intent.putExtra("USERNAME", enteredUseame);
intent.putExtra("MESSAGE", "You have been successfully login");
startActivity(intent);
}
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = br.readLine()) != null) {
answer.append(rLine);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
retu answer;
}
}
private int retuParsedJsonObject(String result){
JSONObject resultObject = null;
int retuedResult = 0;
try {
resultObject = new JSONObject(result);
retuedResult = resultObject.getInt("success");
} catch (JSONException e) {
e.printStackTrace();
}
retu retuedResult;
}
}
but there is red lines under the http request code :-
protected String doInBackground(String... params) {
HttpParams httpParameters = new BasicHttpParams();
HttpCoectionParams.setCoectionTimeout(httpParameters, 5000);
HttpCoectionParams.setSoTimeout(httpParameters, 5000);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost(params[0]);
String jsonResult = "";
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("useame", params[1]));
nameValuePairs.add(new BasicNameValuePair("password", params[2]));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
retu jsonResult;
}
there is red line under BasicHttpParams and under HttpClient and under HttpPost .....etc. when i run the application these errors appear :-
Error:(68, 45) error: caot find symbol class BasicHttpParams Error:(72, 13) error: caot find symbol class HttpClient Error:(72, 41) error: caot find symbol class DefaultHttpClient Error:(73, 37) error: caot find symbol class HttpPost Error:(77, 22) error: caot find symbol class NameValuePair Error:(80, 40) error: caot find symbol class UrlEncodedFormEntity Error:(77, 68) error: caot find symbol class NameValuePair Error:(79, 40) error: caot find symbol class BasicNameValuePair Error:(82, 17) error: caot find symbol class HttpResponse Error:(85, 22) error: caot find symbol class ClientProtocolException Error:(73, 13) error: caot find symbol class HttpPost Error:(78, 40) error: caot find symbol class BasicNameValuePair
what's the problem and how i can fix it ? help me and thanks
