I just started learning android few days ago and I have a problem with uploading my JSON data to server. I manage to retrieve it via following code:
Edit: I did manage to retrieve files using external OKHTTP library but I would like to do this without using external libraries.
package cc.demorest; import android.os.AsyncTask; import android.renderscript.ScriptGroup; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.EditText; import android.widget.TextView; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DownloadTask task = new DownloadTask(); task.execute("myserver.com"); } //Downloadtask public class DownloadTask extends AsyncTask<String,Void,String> { @Override protected String doInBackground(String... urls) { String result = ""; URL url; HttpURLConnection urlConnection=null; try { url = new URL(urls[0]); urlConnection=(HttpURLConnection)url.openConnection(); InputStream in = urlConnection.getInputStream(); InputStreamReader reader = new InputStreamReader(in); int data=reader.read(); while (data !=-1){ char current=(char) data; result += current; data = reader.read(); } return result; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } //After download task @Override protected void onPostExecute(String result) { super.onPostExecute(result); try { JSONArray jArray=new JSONArray(result); JSONObject json_data = jArray.getJSONObject(1); //Logging data Log.i("Podatci: ", "Id: " + json_data.getInt("Id") + ", Name: " + json_data.getString("Name") + ", Years: " + json_data.getString("Age") + ", Email address: " + json_data.getString("Email") ); TextView textView = (TextView) findViewById(R.id.textViewName); textView.setText("ID: "+", Name: "+ json_data.getInt("Id")+json_data.getString("Name")+json_data.getString("Age")+json_data.getString("Email")); /* String data = jsonObject.getString("Name"); Log.i("Website content", data); */ } catch (JSONException e) { e.printStackTrace(); } } } } I am now trying to send my data to same server with same fields. I searched internet but most things that I found are outdated.. I would really appriciate some help or example. Best Regards