get data from remote server in android

后端 未结 2 554
花落未央
花落未央 2021-01-17 00:44

I am using oracle db and weblogic as web server. From my android app i am able to send parameters to my remote server.. My PROBLEM is HOW TO GET THE RESULT SET GENERATED BY

2条回答
  •  [愿得一人]
    2021-01-17 01:37

    The code to be witten in android should be this:

    package com.campuspro.start;
    
    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.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    
    public class Login_Menu extends Activity {
    
    EditText usname;
    EditText pass;
    TextView tv;
    HttpClient client;
    HttpPost post;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_lay);
     tv=(TextView) findViewById(R.id.login_stat_tv);
     usname=(EditText)findViewById(R.id.uname);
     pass=(EditText)findViewById(R.id.pass);
    Button login=(Button)findViewById(R.id.login_but);
    Button cancel=(Button)findViewById(R.id.cancel_but);
    
    client = new DefaultHttpClient();
    String url="http://10.0.2.2:7001/proj/login.jsp";//your url
    post = new HttpPost(url);
    login.setOnClickListener(new View.OnClickListener() {
    
        public void onClick(View arg0) {
            new Login().execute("");
        }
    });
    
    cancel.setOnClickListener(new View.OnClickListener() {
    
        public void onClick(View v) {
            usname.getText().clear();
            pass.getText().clear();
        }
    });
    
     }
    
    
    
    
    private class Login extends AsyncTask{
    ProgressDialog dialog = ProgressDialog.show(Login_Menu.this, "", "Authenticating, Please wait...");
    
    @Override
    protected JSONObject doInBackground(String... params) {
        Log.i("thread", "Doing Something...");
       //authentication operation
    try{
    
        List pairs = new ArrayList();   
        pairs.add(new BasicNameValuePair("username",usname.getText().toString()));   
        pairs.add(new BasicNameValuePair("password",pass.getText().toString()));   
        post.setEntity(new UrlEncodedFormEntity(pairs));   
        HttpResponse response = client.execute(post);
        int status=response.getStatusLine().getStatusCode();
    
        if(status == 200)
        {
            HttpEntity e=response.getEntity();
            String data=EntityUtils.toString(e);
            JSONObject last=new JSONObject(data);
            return last;
    
        }
    
    }
    
    catch(Exception e)
    {
        e.printStackTrace();   
    
    }
    
        return null;
    }
    
    protected void onPreExecute(){
        //dialog.dismiss();
        Log.i("thread", "Started...");
        dialog.show();
    }
    protected void onPostExecute(JSONObject result){
        Log.i("thread", "Done...");
        String status;
        String name;
        try {
            status= result.getString("status");
            name=result.getString("uname");
    
           if(dialog!=null)
           {
             dialog.dismiss();
           }
           else{}
    
         if(status.equalsIgnoreCase("yes"))
              {
            tv.setText("Login Successful...");
    
            Bundle newbundle=new Bundle();
            newbundle.putString("uname",name);
    
            Intent myIntent=new Intent(Login_Menu.this,Instruction.class);
            myIntent.putExtras(newbundle);
    
            startActivity(myIntent);
    
            }
          else{
    
                tv.setText("No User Found, please try again!");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
      }
    
    }
    
    }
    

    And the server side JSP code should be:

    <%@page contentType="text/html; charset=UTF-8"%>
    <%@page import="org.json.simple.JSONObject"%>
    <%@page import="java.util.*,java.sql.*"%>
    
    <%!
    
    Connection con;
    PreparedStatement ps;
    ResultSet rs;
    String x,y;
    
    %>
    
    <%
     Class.forName("oracle.jdbc.driver.OracleDriver");
    
     con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","db_username","db_password");
    x=request.getParameter("username");
    y=request.getParameter("password");
    
    ps=con.prepareStatement("select * from table_name where suid=? and spwd=?");
    ps.setString(1,x);
    ps.setString(2,y);
    rs=ps.executeQuery();
    
    
    JSONObject obj=new JSONObject();
    if(rs.next())
    {
    String uname=rs.getString(4);
    obj.put("status","yes");
    obj.put("uname",uname);
    out.print(obj);
    
    }
    else
    {
    obj.put("status","no");
    out.print(obj);
    }
    out.flush();
     %>
    

提交回复
热议问题