Witam !
Robię aplikację z systemem logowania. Polega to na tym że po wciśnięciu przycisku dane z pól tekstowych loginu i hasła wysyłane są metodą GET do serwera gdzie strona php wyszukuje dane w bazie i zwraca dane w postaci json. no i wszystko fajnie, zostaje tylko pobrać dane. No niestety w moim przypadku tak nie jest. Z poradników w internecie sklekotałem taki oto kod.
public class singIn extends Activity {
SharedPreferences preferences;
SharedPreferences.Editor editor;
private Button msingIn;
private TextView registerLink;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sing_in);
msingIn = (Button) findViewById(R.id.singIn);
registerLink = (TextView) findViewById(R.id.registerLink);
registerLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent isingup = new Intent(getApplicationContext(), singUp.class);
startActivity(isingup);
}
});
msingIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AsyncTaskParseJson a = new AsyncTaskParseJson();
a.execute();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_sing_in, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
public EditText musername;
public EditText mpassword;
protected void onCreate(Bundle savedInstanceState) {
musername = (EditText) findViewById(R.id.username);
mpassword = (EditText) findViewById(R.id.password);
}
@Override
protected void onPreExecute() {}
@Override
protected String doInBackground(String... arg0) {
try {
JsonParser jParser = new JsonParser();
JSONObject json = jParser.getJSONFromUrl("http://stronka.xaa.pl/login.php?username=" + musername + "&password=" + mpassword);
// Storing each json item in variable
String succes = json.getString("succes");
int duration = Toast.LENGTH_LONG;
// show the values in our logcat
if (succes == "1") {
Toast.makeText(getApplicationContext(), "Prawidłowo !", duration).show();
} else if (succes == "0") {
Toast.makeText(getApplicationContext(), "Nieprawidłowe dane !", duration).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String strFromDoInBg) {}
}
}
i klaseJsonParser.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class JsonParser {
final String TAG = "JsonParser.java";
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONObject getJSONFromUrl(String url) {
// make HTTP request
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e(TAG, "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e(TAG, "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
Co robię źle ?
Proszę o pomoc 😃
A i jeszcze w jakiej postaci jest json może sie przyda
{"succes":"1","id":"1","username":"sewek","password":"hasło","email":"-.-@gmail.com"}