Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6836111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:18:58+00:00 2026-05-26T23:18:58+00:00

Possible Duplicate: Sending and Parsing JSON in Android I am fetching JSON Data from

  • 0

Possible Duplicate:
Sending and Parsing JSON in Android

I am fetching JSON Data from server and I have already got JSON Data by code. But I didn’t understand how to parse it.

Here is my code for server i am fetching data from mysql database and then sending to android
device

   import java.io.*;
   import java.util.*;
   import javax.sql.*;
   import javax.servlet.*;
   import javax.servlet.http.*;
   import java.sql.Connection;
   import java.sql.DriverManager;
   import java.sql.ResultSet;
   import java.sql.SQLException;
   import java.sql.Statement;
   import net.sf.json.JSONArray;

   public class DBConnection extends HttpServlet {
          public void doGet(HttpServletRequest request,HttpServletResponse     response)throws IOException, ServletException{

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

    // connecting to database
    Connection con = null;    
    Statement stmt = null;
    ResultSet rs = null;

    try {
        Class.forName("com.mysql.jdbc.Driver");
        con =DriverManager.getConnection("jdbc:mysql://localhost:3306/emarnew", "root","12345");
        stmt = con.createStatement();
        rs = stmt.executeQuery("SELECT * FROM allpatientdetails WHERE patientname='Mr Hans Tan'");


    // displaying records
        while(rs.next()){
    JSONArray arrayObj=new JSONArray();
        String name="patientname"+rs.getObject(1).toString();
    arrayObj.add(name);

        arrayObj.add(rs.getObject(2).toString());
        arrayObj.add(rs.getObject(3).toString());
        arrayObj.add(rs.getObject(4).toString());
        arrayObj.add(rs.getObject(5).toString());
        arrayObj.add(rs.getObject(6).toString());
        arrayObj.add(rs.getObject(7).toString());
        arrayObj.add(rs.getObject(8).toString());
        String nik =arrayObj.toString();

           // fetch the parameters from client..

            final String user = request.getParameter("UID");
             // out.print(user);

             if("patients".equals(user))
               out.print(nik);
            else 
               out.println("fail");

         // out.print(nik);
           //   out.print("\t\t\t");
             //   out.print("<br>");

}
    } catch (SQLException e) {
        throw new ServletException("Servlet Could not display records.", e);
      } catch (ClassNotFoundException e) {
          throw new ServletException("JDBC Driver not found.", e);
        } finally {
            try {
                if(rs != null) {
                    rs.close();
                    rs = null;
                }
                if(stmt != null) {
                    stmt.close();
                    stmt = null;
                }
                if(con != null) {
                    con.close();
                    con = null;
                }
            } catch (SQLException e) {}
        }

        out.close();
    }

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException
{
    this.doGet(request, response);
}


}

And here is my android code
import java.util.ArrayList;
import java.util.List;

       import org.apache.http.HttpEntity;
        import org.apache.http.HttpResponse;
        import org.apache.http.NameValuePair;
       import org.apache.http.client.HttpClient;
        import org.apache.http.client.entity.UrlEncodedFormEntity;
          import org.apache.http.client.methods.HttpGet;
       import org.apache.http.client.methods.HttpPost;
             import org.apache.http.impl.client.DefaultHttpClient;
         import org.apache.http.message.BasicNameValuePair;
         import org.apache.http.protocol.HTTP;
        import org.apache.http.util.EntityUtils;

      import android.app.Activity;
       import android.os.Bundle;
       import android.util.Log;
       import android.widget.TextView;

     public class ClientActivity extends Activity 
      {
TextView tv;
public  StringBuffer buffer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tv= (TextView)findViewById(R.id.tv);

    try {
        HttpClient client = new DefaultHttpClient();  
        String postURL = "http://10.0.2.2:8080/nik/DBConnection?UID=patients";
        HttpPost post = new HttpPost(postURL);
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("UID", "patients"));
            //params.add(new BasicNameValuePair("pass", "xyz"));
            UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
            post.setEntity(ent);
            HttpResponse responsePOST = client.execute(post);  
            HttpEntity resEntity = responsePOST.getEntity();  
            if (resEntity != null) {    
                //Log.i("RESPONSE",EntityUtils.toString(resEntity));
                buffer = new StringBuffer("");
                buffer.append(EntityUtils.toString(resEntity));

            }

           String String1= buffer.toString();
           Log.i("string-----",String1);
           tv.append(String1);



    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        HttpClient client = new DefaultHttpClient();  
        String getURL = "http://10.0.2.2:8080/nik/DBConnection?UID=patients";
        HttpGet get = new HttpGet(getURL);
        HttpResponse responseGet = client.execute(get);  
        HttpEntity resEntityGet = responseGet.getEntity();  
        if (resEntityGet != null) {  
                    //do something with the response
                    Log.i("GET RESPONSE",EntityUtils.toString(resEntityGet));

                }
} catch (Exception e) {
    e.printStackTrace();
}
}
}

And this is the result that i am getting Mr Hans Tan”,”S2234567″,”75945422″,”20-11-1971″,”Chronic Hepatatis C”,”Copegus 400 mg”,”8″,”Ward3A-1,301″ ,but i need to parse it so please any help will be appreciated .Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:18:59+00:00Added an answer on May 26, 2026 at 11:18 pm

    You’ll use the JSONObject and/or JSONArray class to parse JSON data on android.

    Basic usage goes like this:

    JSONObject jsonObject = new JSONObject(myJsonDataString);
    

    and

    JSONArray jsonArray = new JSONArray(myJsonDataString);
    

    You can find more info on the usage at the Android Developers Page, specifically here and here.

    Be careful: data in the format: “[{‘key’:”value’}]” (I’m talking about the []’s) has to be parsed with JSONArray or you’ll get a JSONException raised saying that your data isn’t a JSONObject.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: sending post data from Iphone Hello everyone, I'm a new member of
Possible Duplicate: Sending Email in Android using JavaMail API without using the default android
Possible Duplicate: sending mail with php & escaping hotmails junk folder I have been
Possible Duplicate: Sending Email in .NET Through Gmail I have so many problems with
Possible Duplicate: A transport-level error has occurred when receiving results from the server A
Possible Duplicate: Sending SMS from PHP I would like to make and develop a
Possible Duplicate: Sending email through Gmail SMTP server with C# I would like my
Possible Duplicate: Sending HTML email from PHP Using PHP to send an email. I
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.