Android & Php: Post-ing Data From Android To Mysql Db Using Php
Solution 1:
First you need to understand error codes. usually error codes in the 400's means you somehow hit the server but you are restricted to access resources or simply you just made bad request by providing wrong data. And error's in the 500's, those are normally sever errors. Which indicate that there is a problem with your server code. Although note this as it's important to understand. When testing in local host, you need to expose your IP for your server to be to handle requests else you won't be able to hit your local host server. If you are a newbie checkout tools like Ngrok, will help you expose your IP faster so you can get the API's done. Goodluck Cheers!
Solution 2:
I figured it out 10minutes ago. I'm posting new code now, and it works.
package com.example.korisnik.test;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
publicclassDbManagerextendsActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
}
publicvoidregister(View v) {
EditTextuname= (EditText) findViewById(R.id.username);
EditTextpw= (EditText) findViewById(R.id.password);
Stringusername= uname.getText().toString();
Stringpassword= pw.getText().toString();
System.out.println("Username is: " + username + " ,and password is: " + password);
Uploadertask=newUploader();
task.execute(newString[] { "http://10.0.2.2/user_db/senddata.php", username, password });
}
publicclassUploaderextendsAsyncTask<String, Void, String> {
@Overrideprotected String doInBackground(String... params) {
Stringresponse=null;
try {
response += postHttpContent(params[0],params[1],params[2]);
} catch (IOException e) {
System.out.println("IO Error");
Log.e("error", e.toString());
} catch (JSONException e) {
System.out.println("JSON Error");
e.printStackTrace();
}
return response;
}
@OverrideprotectedvoidonPostExecute(String result) {
System.out.println(result);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
public String postHttpContent(String urlStr, String user, String pass)throws IOException, JSONException {
Stringresponse="";
URLurl=newURL(urlStr);
HttpURLConnectionhttpConn= (HttpURLConnection) url.openConnection();
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
httpConn.setRequestMethod("POST");
JSONObjectuserdata=newJSONObject();
userdata.put("username",user);
userdata.put("password", pass);
JSONArraydata=newJSONArray();
data.put(userdata);
System.out.println("Array is: " + data);
try {
DataOutputStreamlocalDataOutputStream=newDataOutputStream(httpConn.getOutputStream());
localDataOutputStream.writeBytes(data.toString());
localDataOutputStream.flush();
localDataOutputStream.close();
System.out.println("Data writting: " + data);
} catch (IOException e) {
e.printStackTrace();
System.out.println("Data Output error");
}
intresponseCode= httpConn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
String line;
BufferedReaderbr=newBufferedReader(newInputStreamReader(httpConn.getInputStream()));
do {
line = br.readLine();
response += line;
} while ((line = br.readLine()) != null);
} else {
response = "Error ";
thrownewIOException();
}
return response + " * Uploaded!";
}
}
}
I used JSONObject and put key/value pairs of username and password in it. Than I put that JSONObject in JSONArray (I know it's just one JSONObject, and I didn't have to put it into JSONArray, but since my PHP code is "designed" for JSONArray, and I'm not good with PHP, I figured it's easier to put JSONObject into JSONArray, than to change PHP code which I don't understand very well.
Second change is using "DataOutputStream" instead of "OutputStreamWriter".
Post a Comment for "Android & Php: Post-ing Data From Android To Mysql Db Using Php"