i am searching for hours in the Internet and i dont know how i can handle this Error. I get empty content from my Inputstream. Maybe some one of you can look at my code where my error is.
public class SigninActivity extends AsyncTask<String,Void,String> {
private TextView testtext;
private Context context;
int code = 0;
View view;
String json = " ";
JSONObject jsonObj;
//flag 0 means get and 1 means post.(By default it is get.)
public SigninActivity(Context context,View view, TextView testtext) {
this.context = context;
this.testtext = testtext;
this.view = view;
}
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... arg0) {
try {
String username = (String) arg0[0];
String password = (String) arg0[1];
String link = "http://ift.tt/1L9RGYe";
String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8");
data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
URL url = new URL(link);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
Log.d("JSON Input stream", json.toString());
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
Log.d("my JSON BufferedReader", json);
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while ((line = reader.readLine()) != null) {
sb.append(line);
Log.d("my JSON Server Response", json.toString());
break;
}
this.json = sb.toString();
return json;
} catch (Exception e) {
Log.e("Login Fail", e.toString());
return new String("Exception: " + e.getMessage());
}
}
//@Override
protected void onPostExecute(String flag) {
this.testtext.setText(flag);
try
{
JSONObject json_data = new JSONObject(flag);
Log.d("my", " JSON Object Parser" );
int code= (json_data.getInt("code"));
Log.d("my", " JSON get code");
Log.d("my", String.valueOf(code));
//int code =1 ;
if(code==1)
{
Toast.makeText(context, "Login Successfully",
Toast.LENGTH_LONG).show();
//Start des Hauptdingens
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
// Start der Register Acitivty;
//startActivityForResult(myIntent, 0);
//startActivity(myIntent);
//ActivityCompat.startActivityForResult(myIntent, 0);
//finish();
}
else
{
Toast.makeText(context, "Sorry, Try Again",
Toast.LENGTH_LONG).show();
}
}
catch(Exception e)
{
Log.e("my JSON Fail", e.toString());
}
}
}
here i have my php file, i am not an expert at php maybe someone of you can help me :
<?php
$con=mysqli_connect("localhost","root","","my_db");
//echo "Welcome, I am connecting Android to PHP, MySQL";
if (mysqli_connect_errno($con))
{
//echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$username = $_POST['username'];
$password = $_POST['password'];
$result = mysqli_query($con,"SELECT Username FROM user where Username='$username' and Password='$password'");
$flag['code'] =0;
if($result)
{
$flag['code']=1;
} else {
$flag['code'] = 0;
}
//print(json_encode($flag));
//echo json_encode($flag)
mysqli_close($con);
?>
i commented the echo lines out, because i read that this could make some errors, but this didnt help. the login with the post methode, but the resonse with flag doesnt work.
thanks for your help
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire