简体   繁体   中英

Problems with connecting to PHP through Android

I am trying to send a JSON object from my android application to my site's database. However, I seem to not get any type of connection and I don't get any exception errors. I am currently trying to get a connection just with java in eclipse but still no luck.

submitAlbum.php:

<?php
if(isset($_POST["albumId"])){
    $albumId = json_decode($_POST["albumId"]);
}

The java Code:

String albumId = "qjlix";

    try{
        //Creates JSON object
        JSONObject tempObj = new JSONObject();
        tempObj.put("albumId", albumId);

        //Creates connection to URL
        URL targetUrl = new URL("http://example.com/submitAlbum.php");
        HttpURLConnection conn = (HttpURLConnection) targetUrl.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setConnectTimeout(500);
        conn.setRequestProperty("Content_Type", "application/json; charset=UTF-8");

        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
        writer.write(tempObj.toString());
        writer.flush();
        writer.close();
    } catch (Exception e){
        System.out.println(e.toString());;
    }

Is their something I am missing or doing wrong?

You are not sending a POST parameter/key 'albumId' of which the value is a json text. Instead you send a json text containing a json parameter 'albumId'.

Php script and client code do not match.

First decide if you want to POST key=value pairs. Or want to POST json text. Then handle accordingly.

I don't know why but the PHP would not even run until run this on the client side:

conn.getResponseCode();

So in the end I just made the PHP code already have a albumId set, it just needed something to connect to it. Then it would submit it to my database.

PHP:

 $albumId = "qjlix";

Then in my java all I needed to do was get a connection the the PHP file.

Java:

 try{
        URL url = new URL("http://example.com/submitAlbum.php");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
        writer.write("albumId = this");
        writer.close();

        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            System.out.println("worked");
        }else{
            System.out.println("Did not work");
        }

    }catch(Exception e){
        System.out.println("exception thrown");
    }

I was testing the Java in IntelliJ even through it's going to be used in an Android application. I plan on just using Volley since HttpURLConnection is weird sometimes. Thanks!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM