0

I'm using a recyclerview to display the list of player. I added a click listener to recycler view. I want to send player_name to server side using PHP. If someone clicks on one of the items. But I don't know why somehow the code doesn't work. Whenever I execute the code, it just jumps to previous activity.

Below is the android code:

@Override
public void onItemClick_TeamAdapter(int position) {
        TeamItems clickedItem = list_items.get(position);

        String url = "http://www.prasaurus.com/trial_db_php/post_data.php";

        player_name = clickedItem.getPlayer_name();
        player_id = clickedItem.getPlayer_id();
        player_DOB = clickedItem.getPlayer_DOB();

        final List<NameValuePair> parameters = new ArrayList<NameValuePair>();
        parameters.add(new BasicNameValuePair("player_name",player_name));
        //parameters.add(new BasicNameValuePair("player_id",player_id));
        //parameters.add(new BasicNameValuePair("player_DOB",player_DOB));

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

        try {
            httppost.setEntity(new UrlEncodedFormEntity(parameters));
            HttpResponse response = httpclient.execute(httppost);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Here is the code for server side php file:

<?php
$servername = "prasaurus.com";
$username = "prasauru_fand";
$password = "MYPASSWORD";
$dbname = "prasauru_trial_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$Name=$_POST['player_name'];

$sql = "INSERT INTO trial_data (player_name)
VALUES ('{$Name}')";
if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?> 

Logcat :

Process: com.prasaurus.app.team_selection, PID: 21966
    android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
        at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
        at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
        at java.net.InetAddress.getAllByName(InetAddress.java:215)
        at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142)
        at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
        at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:370)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
        at com.prasaurus.app.team_selection.Team1SquadSelection.onItemClick_TeamAdapter(Team1SquadSelection.java:191)
        at com.prasaurus.app.team_selection.TeamAdapter$Viewholder$1.onClick(TeamAdapter.java:81)
        at android.view.View.performClick(View.java:5215)
        at android.view.View$PerformClick.run(View.java:21193)
        at android.os.Handler.handleCallback(Handler.java:742)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5571)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)

I'm using Onclick listener on RecyclerView Adapter to select the item.

12
  • Possible duplicate of How to send variable from php to an android app? Commented Oct 12, 2018 at 10:41
  • @MasivuyeCokile I've already tried that still now working :( Commented Oct 12, 2018 at 10:45
  • What is the result of your debugging work? Are any of the Exceptions catched? Is a http connection established? Is the php script itself ok and running? Did you set breakpoints in android to see how far you get? ...? Commented Oct 12, 2018 at 11:01
  • tell me any authentic player name ? Commented Oct 12, 2018 at 11:23
  • 1
    have you given network permission in manifest Commented Oct 12, 2018 at 11:34

1 Answer 1

2

Okay So NetworkOnMainThreadException comes when you try to do some thing on Main Thread which should be on Background thread. You can transfer your code to AsyncTask . error will be resolved.

public class backgroundTask extends AsyncTask<Void, Void, Void> {

@Override
protected Void doInBackground(Void... voids) {
    String url = "http://www.prasaurus.com/trial_db_php/post_data.php";

    player_name = clickedItem.getPlayer_name();
    player_id = clickedItem.getPlayer_id();
    player_DOB = clickedItem.getPlayer_DOB();

    final List<NameValuePair> parameters = new ArrayList<NameValuePair>();
    parameters.add(new BasicNameValuePair("player_name",player_name));
    //parameters.add(new BasicNameValuePair("player_id",player_id));
    //parameters.add(new BasicNameValuePair("player_DOB",player_DOB));

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);

    try {
        httppost.setEntity(new UrlEncodedFormEntity(parameters));
        HttpResponse response = httpclient.execute(httppost);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
protected void onPostExecute(String aVoid) {
    super.onPostExecute(aVoid);
}
}

For calling this method. use

new backgroundTask().execute();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.