Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid connection with mysql using json
    primarykey
    data
    text
    <p>JSON-MYSQL connectivity with 2 text fields [name and comments] in a mysql table named 'emp' in my server .. and it woks fine ..but now I want to store image , with an extra field... but don't know how..pls help.. These is my follwing code..</p> <p><strong>MainActivity.java</strong></p> <pre><code>package com.example.jparser; import java.io.InputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ByteArrayEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.json.JSONObject; import android.annotation.TargetApi; import android.app.Activity; import android.os.Build; import android.os.Bundle; import android.support.v4.app.NavUtils; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class AddnewActivity extends Activity { EditText name; EditText comments; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_addnew); // Show the Up button in the action bar. setupActionBar(); Button save=(Button) findViewById(R.id.button1); save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try{ name=(EditText) findViewById(R.id.editText1); String nm=name.getText().toString(); comments=(EditText) findViewById(R.id.editText2); String com=comments.getText().toString(); if("".equals(nm) || "".equals(com)){ Toast.makeText(getApplicationContext(), "Empty field detected", Toast.LENGTH_SHORT).show(); } else{ try { JSONObject json = new JSONObject(); json.put("name", nm); json.put("comments", com); HttpClient client = new DefaultHttpClient(); String url = "http://share88.hostzi.com/parser/json_req.php"; HttpPost request = new HttpPost(url); request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8"))); request.setHeader("json", json.toString()); HttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); Toast.makeText(getApplicationContext(), "Inserted", Toast.LENGTH_SHORT).show(); } name.setText(null); comments.setText(null); } catch (Throwable t) { Toast.makeText(getApplicationContext(), "Request failed: " + t.toString(), Toast.LENGTH_SHORT).show(); } } }catch(Exception e){} } }); } /** * Set up the {@link android.app.ActionBar}, if the API is available. */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) private void setupActionBar() { if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.HONEYCOMB) { getActionBar().setDisplayHomeAsUpEnabled(true); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.addnew, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // This ID represents the Home or Up button. In the case of this // activity, the Up button is shown. Use NavUtils to allow users // to navigate up one level in the application structure. For // more details, see the Navigation pattern on Android Design: // // http://developer.android.com/design/patterns/navigation.html#up-vs-back // NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); } } </code></pre> <p><strong>activity_addnew.xml</strong></p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".AddnewActivity" &gt; &lt;EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="16dp" android:ems="10" android:inputType="text" /&gt; &lt;Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/editText2" android:layout_marginTop="48dp" android:text="@string/addnewcontent" /&gt; &lt;EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView2" android:layout_marginTop="17dp" android:ems="10" android:inputType="textPostalAddress" /&gt; &lt;TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText2" android:layout_below="@+id/editText1" android:layout_marginTop="48dp" android:text="@string/addcomments" /&gt; &lt;TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="30dp" android:text="@string/addname" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p><strong>AddnewActivity.java</strong></p> <pre><code>package com.example.jparser; import java.io.InputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ByteArrayEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.json.JSONObject; import android.annotation.TargetApi; import android.app.Activity; import android.os.Build; import android.os.Bundle; import android.support.v4.app.NavUtils; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class AddnewActivity extends Activity { EditText name; EditText comments; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_addnew); // Show the Up button in the action bar. setupActionBar(); Button save=(Button) findViewById(R.id.button1); save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try{ name=(EditText) findViewById(R.id.editText1); String nm=name.getText().toString(); comments=(EditText) findViewById(R.id.editText2); String com=comments.getText().toString(); if("".equals(nm) || "".equals(com)){ Toast.makeText(getApplicationContext(), "Empty field detected", Toast.LENGTH_SHORT).show(); } else{ try { JSONObject json = new JSONObject(); json.put("name", nm); json.put("comments", com); HttpClient client = new DefaultHttpClient(); String url = "http://share88.hostzi.com/parser/json_req.php"; HttpPost request = new HttpPost(url); request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8"))); request.setHeader("json", json.toString()); HttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); Toast.makeText(getApplicationContext(), "Inserted", Toast.LENGTH_SHORT).show(); } name.setText(null); comments.setText(null); } catch (Throwable t) { Toast.makeText(getApplicationContext(), "Request failed: " + t.toString(), Toast.LENGTH_SHORT).show(); } } }catch(Exception e){} } }); } /** * Set up the {@link android.app.ActionBar}, if the API is available. */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) private void setupActionBar() { if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.HONEYCOMB) { getActionBar().setDisplayHomeAsUpEnabled(true); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.addnew, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // This ID represents the Home or Up button. In the case of this // activity, the Up button is shown. Use NavUtils to allow users // to navigate up one level in the application structure. For // more details, see the Navigation pattern on Android Design: // // http://developer.android.com/design/patterns/navigation.html#up-vs-back // NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); } } </code></pre> <p>And my PHP files are</p> <p><strong>json_res.php</strong></p> <pre><code>&lt;?php include "connect.php"; $result=mysql_query("SELECT* FROM emp"); while($row=mysql_fetch_assoc($result)){ $output[]=$row; } print(json_encode($output)); mysql_close(); ?&gt; </code></pre> <p>and <strong>json_req.php</strong></p> <pre><code>&lt;?php $json = file_get_contents('php://input'); $obj = json_decode($json); include "connect.php"; mysql_query("INSERT INTO emp VALUES ('','".$obj-&gt;{'name'}."', '".$obj-&gt;{'comments'}."')"); mysql_close($con); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload