0

What I'm trying to do is send a current facebook user's profile from android to a web server. (I'm using PHP) I have some problem that I could not solve it for 7 days.. I tried many times.. but I couldn't make it. Can anybody possibly help me how to figure it out? The result shows a null pointer exception..

Client UPDATED!!

public static JSONObject Connection() throws Exception{

    String TAG = "ConnectionToServer";
    HttpURLConnection conn = null;
    OutputStream out = null;
    BufferedWriter writer = null;
    GraphUser user = null;
    JSONObject parameter = null;


    try{

        parameter = new JSONObject();
        parameter.put("id", user.getId()); //first null exception occurs..
        Log.d(TAG,"ok1");
        parameter.put("name",  user.getName());
        Log.d(TAG,"ok2");
        parameter.put("gender", user.getProperty("gender"));
        parameter.put("location",  user.getLocation().getProperty("name"));
        parameter.put("locale",  user.getProperty("locale"));
        parameter.put("birthday",  user.getBirthday());
        parameter.put("email",  user.getProperty("email"));

        URL url = new URL("http://sadamell.ncity.net/www/jeffcodes/FbUser.php");
        conn = (HttpURLConnection) url.openConnection();
        Log.d(TAG,"ok3");
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setDoOutput(true); //we will send stuff
        conn.setUseCaches(false); //no caches
        conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
        conn.connect();
        Log.d(TAG,"Connection Success!");

        out = new BufferedOutputStream(conn.getOutputStream());
        writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
        writer.write(parameter.toString());
        writer.close();
        out.close();
        conn.disconnect();
    }catch(JSONException e){
        e.printStackTrace();
        }
    catch(IOException e){
        e.printStackTrace();
    }
    return parameter;
}
Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78

2 Answers2

0

You never instanciate jsonarray:

JSONArray jsonarray = null;
jsonarray.put(parameter);

This could be the reason for your null pointer exception. Change this to:

JSONArray jsonarray = new JSONArray();

Second (as BlackJack pointed out) your user object is null too.

Additional: I think your userInfo.append() calls don't work the way you think. Every parameter you add gets all previous entries also.

Another heads up: I suspect when you fix your current problem you will run into an NetworkOnMainThreadException...

Community
  • 1
  • 1
Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
  • I've modified my code what you just told me to do.. However, it's still showing NullPointException. –  Jul 11 '14 at 08:48
  • $json_object = $_POST["ja"]; $emp_data = json_decode($json_object,true); $fb_id = $emp_data->id; $fb_name = $emp_data->name; $gender = $emp_data->gender; $location = $emp_data->location; $locale = $emp_data->locale; $birthday = $emp_data->birthday; $email = $emp_data->email; $sql = "INSERT INTO fb_users(user_fb_id,name,gender,location,locale,birthday,email) VALUES('$fb_id','$fb_name','$gender','$location','$locale','$birthday','$email')"; is there anything wrong with my php code? –  Jul 11 '14 at 08:50
  • @JeffMinsungKim Update your question to show your current state of the code and add where the exeptions exactly occur. – Gerald Schneider Jul 11 '14 at 08:50
  • I have updated my code.. please take a look at it..:( –  Jul 11 '14 at 09:20
  • I fixed the current problem and you were right!! NetworkOnMainThreadException occurs right now.........do you know any solutions? –  Jul 11 '14 at 09:58
0

Use this code to send your data to the provided url and set the time out and other thing whatever you need.

         ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("location",inputSearch.getText().toString()));
          nameValuePairs.add(new BasicNameValuePair("id", userInfo.append(user.getId()));
            nameValuePairs.add(new BasicNameValuePair("name",  userInfo.append(user.getName()));
            nameValuePairs.add(new BasicNameValuePair("gender", userInfo.append(user.getProperty("gender")));
             nameValuePairs.add(new BasicNameValuePair("location",  userInfo.append(user.getLocation().getProperty("name")));
             nameValuePairs.add(new BasicNameValuePair("locale",  userInfo.append(user.getProperty("locale")));
             nameValuePairs.add(new BasicNameValuePair("birthday",  userInfo.append(user.getBirthday()));
             nameValuePairs.add(new BasicNameValuePair("email",  userInfo.append(user.getProperty("email")));

           try{
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost("http://mydomain.com/test.php");
                        httppost.setEntity(new UrlEncodedFormEntity(get_nameValuePairs));
                        HttpResponse response = httpclient.execute(httppost);
                        HttpEntity entity = response.getEntity();
                        is = entity.getContent();
                    }catch(Exception e){
                        Log.e("log_tag", "Error in http connection "+e.toString());
                    }
                    //convert response to string
                    try{
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                        StringBuilder sb = new StringBuilder();
                        String line = null;
                        while ((line = reader.readLine()) != null) {
                            sb.append(line + "\n");
                        }
                        is.close();

                        result=sb.toString();
                        //System.out.println("query Result:----------"+result);
                    }catch(Exception e){
                        Log.e("log_tag", "Error converting result "+e.toString());
                    }

    try{
                        JSONArray jArray = new JSONArray(dd);
                        for(int i=0;i<jArray.length();i++){
                            JSONObject json_data = jArray.getJSONObject(i);
                            HashMap<String, String> hm = new HashMap<String,String>();
                            Log.d("",json_data.GetString("Index_name"))


                        }


                    }catch(JSONException e){
                        Log.e("log_tag", "Error parsing data "+e.toString());
                        Log.e("log_tag", "Failed data was:\n" + dd);
                    }

           return jArray;
Laxmeena
  • 780
  • 2
  • 7
  • 28
  • @JeffMinsungKim, Then You can use JSONArray part which is inside try block into your code, It will work.... – Laxmeena Jul 11 '14 at 09:35
  • I'm trying to send those json data to a server. so that the server could insert into mysql database. –  Jul 11 '14 at 09:44
  • @JeffMinsungKim, don't do that much only send the arraylist(namePairValue) and accept on server according to field wise. same thing i'm using in my project currently and its just working great... – Laxmeena Jul 11 '14 at 09:54
  • could you please email me your source code that relate to sending data to the server.. It's hard for me to understand the point.. ziznzkak@gmail.com –  Jul 12 '14 at 14:06