1 Replies - 307 Views - Last Post: 28 March 2019 - 09:30 AM

#1 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 291
  • Joined: 13-March 18

app pulling null from link

Posted 28 March 2019 - 08:18 AM

I had this working perfectly previously and i dont know what changed that would result it returning null.

this is my backwork class that handles async taks.

public class BackWork extends AsyncTask<String,String,String> {
    Context context;
    String type;
    BackWork (Context context){
        this.context = context;
    }

    @Override
    protected String doInBackground(String... params) {
        type = params[0];
        String login_url = "";
        if(type.equals("login")){
            try {
                login_url = "http://ip/app/login.php?type=login";
                String user = params[1];
                String pass = params[2];
                URL url = new URL(login_url);
                HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
                urlconn.setRequestMethod("POST");
                urlconn.setDoOutput(true);
                urlconn.setDoInput(true);
                OutputStream out = urlconn.getOutputStream();
                BufferedWriter bWrite = new BufferedWriter(new OutputStreamWriter(out,"UTF-8"));
                String post_data = URLEncoder.encode("user","UTF-8")+"="+URLEncoder.encode(user,"UTF-8")+"&"+URLEncoder.encode("pass","UTF-8")+"="+URLEncoder.encode(pass,"UTF-8");
                bWrite.write(post_data);
                bWrite.flush();
                bWrite.close();
                out.close();
                InputStream in = urlconn.getInputStream();
                BufferedReader bRead = new BufferedReader(new InputStreamReader(in,"iso-8859-1"));
                String result = "";
                String line = "";
                while((line = bRead.readLine()) != null){
                    result += line;
                }
                bRead.close();
                in.close();
                urlconn.disconnect();
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else if(type.equals("server")){
            try {
                login_url = "http://ip/app/login.php?type=server";
                URL url = new URL(login_url);
                HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
                InputStream in = urlconn.getInputStream();
                BufferedReader bRead = new BufferedReader(new InputStreamReader(in,"iso-8859-1"));
                String result = "";
                String line = "";
                while((line = bRead.readLine()) != null){
                    result += line;
                }
                bRead.close();
                in.close();
                urlconn.disconnect();
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else if(type.equals("tx")){
            try {
                login_url = "http://ip/app/login.php?type=tx";
                String txtype = params[1];
                String coin = params[2];
                String bid = params[3];
                String tx = params[4];
                String amount = params[5];
                String balance = params[6];
                String date = params[7];
                URL url = new URL(login_url);
                HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
                urlconn.setRequestMethod("POST");
                urlconn.setDoOutput(true);
                urlconn.setDoInput(true);
                OutputStream out = urlconn.getOutputStream();
                BufferedWriter bWrite = new BufferedWriter(new OutputStreamWriter(out,"UTF-8"));
                String post_data = URLEncoder.encode("txtype","UTF-8")+"="+URLEncoder.encode(txtype,"UTF-8")+"&"+ URLEncoder.encode("coin","UTF-8")+"="+URLEncoder.encode(coin,"UTF-8")+"&"+ URLEncoder.encode("bid","UTF-8")+"="+URLEncoder.encode(bid,"UTF-8")+"&"+ URLEncoder.encode("tx","UTF-8")+"="+URLEncoder.encode(tx,"UTF-8")+"&"+ URLEncoder.encode("amount","UTF-8")+"="+URLEncoder.encode(amount,"UTF-8")+"&"+ URLEncoder.encode("balance","UTF-8")+"="+URLEncoder.encode(balance,"UTF-8")+"&"+ URLEncoder.encode("date","UTF-8")+"="+URLEncoder.encode(date,"UTF-8");
                bWrite.write(post_data);
                bWrite.flush();
                bWrite.close();
                out.close();
                InputStream in = urlconn.getInputStream();
                BufferedReader bRead = new BufferedReader(new InputStreamReader(in,"iso-8859-1"));
                String result = "";
                String line = "";
                while((line = bRead.readLine()) != null){
                    result += line;
                }
                bRead.close();
                in.close();
                urlconn.disconnect();
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPreExecute(){
    }

    @Override
    protected void onPostExecute(String result){
        String array[] = new String[12];
        Toast.makeText(context,"This result is "+result,Toast.LENGTH_LONG).show();
        if(type.equals("login")) {
            if (result.equals("-1")) {
                Toast.makeText(context, "No such account", Toast.LENGTH_SHORT).show();
            } else {
                try {
                    JSONObject jresponse = new JSONObject(result);
                    array[0] = jresponse.getString("bid");
                    array[1] = jresponse.getString("name");
                    array[2] = jresponse.getString("type");
                    array[3] = jresponse.getString("status");
                    array[4] = jresponse.getString("address");
                    array[5] = jresponse.getString("base");
                    array[6] = jresponse.getString("btc");
                    array[7] = jresponse.getString("bch");
                    array[8] = jresponse.getString("eth");
                    array[9] = jresponse.getString("ltc");
                    array[10] = jresponse.getString("xmr");
                    array[11] = jresponse.getString("dash");
                    MainPage.account = array;
                    Toast.makeText(context, "Welcome, "+array[1], Toast.LENGTH_SHORT).show();
                } catch (JSONException e) {
                    Toast.makeText(context, "Unknown login Error: "+e, Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }
        }else if(type.equals("server")){
            //Toast.makeText(context, "Result is "+result, Toast.LENGTH_SHORT).show();
            /*
            if (result.equals("-1")) {
                Toast.makeText(context, "Server config not found", Toast.LENGTH_SHORT).show();
            } else {
                try {
                    JSONObject jresponse = new JSONObject(result);
                    array[0] = jresponse.getString("provider");
                    array[1] = jresponse.getString("rates");
                    array[2] = jresponse.getString("fee");
                    array[3] = jresponse.getString("btc");
                    array[4] = jresponse.getString("bch");
                    array[5] = jresponse.getString("eth");
                    array[6] = jresponse.getString("ltc");
                    array[7] = jresponse.getString("xmr");
                    array[8] = jresponse.getString("dash");
                    array[9] = jresponse.getString("status");
                    array[10] = jresponse.getString("contact");
                    array[11] = jresponse.getString("version");
                    Toast.makeText(context, "Server ported", Toast.LENGTH_SHORT).show();
                    MainPage.server = array;
                } catch (JSONException e) {
                    Toast.makeText(context, "Unknown server error: "+e, Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }
            */
        }else if(type.equals("tx")){
            if(result.equals("-1")) {
                Toast.makeText(context, "Required information could not be collected. CoinPay has been notified of this, and may contact you regarding this.", Toast.LENGTH_SHORT).show();
            }else if(result.equals("-2")){
                Toast.makeText(context, "This business account does not exist.", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(context, "Transaction recorded", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onprogressUpdate(String... values){

    }
}



this is my PHP script to return JSON

<?PHP
$db = "db";
$db_user = "root";
$db_pass = "";
$db_host = "localhost";
$conn = mysqli_connect($db_host,$db_user,$db_pass,$db);
if(!empty($_GET['type'])){$type = $_GET['type'];}else{$type = '';}
if($type == 'server'){
	$result = mysqli_query($conn,"SELECT * FROM server")or die(mysqli_error($conn));
	if(mysqli_num_rows($result) > 0){
		$fetch = mysqli_fetch_array($result,MYSQLI_ASSOC);
		echo json_encode($fetch);
	}else{
		echo '-1';
	}
}elseif($type == 'login'){
	$_user = $_POST['user'];
	$_pass = $_POST['pass'];
	if(!$_user || !$_pass){
		echo 'No input';
	}else{
		$result = mysqli_query($conn,"SELECT bid, name, type, status, address, base, btc, bch, eth, ltc, xmr, dash FROM account WHERE name='$_user' AND pass='$_pass'")or die(mysqli_error($conn));
		if(mysqli_num_rows($result) > 0){
			$fetch = mysqli_fetch_array($result,MYSQLI_ASSOC);
			echo json_encode($fetch);
		}else{
			echo '-1';
		}
	}
}elseif($type == 'tx'){
	$txtype = $_POST['txtype'];
	$coin = $_POST['coin'];
	$bid = $_POST['bid'];
	$tx = $_POST['tx'];
	$amount = $_POST['amount'];
	$balance = $_POST['balance'];
	$date = $_POST['date'];
	if(!$txtype || !$coin || !$bid || !$tx || !$amount || !$balance || !$date){
		echo '-1';
	}elseif(mysql_num_rows(mysql_query("SELECT id FROM account WHERE bid='$bid'")) == 0){
		echo '-2';
	}else{
		mysql_query($conn,"INSERT INTO transaction VALUES ('$txtype','$coin','$bid','$tx','$amount','$balance','$date')")or die(mysql_error($conn));
		echo 'Success';
	}
}
?>



When i run the program with this as launcher...

public class Loading extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading);
        BackWork bwork = new BackWork(this);
        bwork.execute("server",null,null);
        //Intent intent = new Intent(this,Login.class);
        //startActivity(intent);
    }
}



it spits out a toast saying result is null.

logcat says...

Connected to process 10697 on device oneplus-oneplus_a3000-b6026923
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/yapp.coinpayap: JIT profile information will not be recorded: profile file does not exits.
I/chatty: uid=10128(com.example.coinpayapp.coinpayapp) identical 3 lines
W/yapp.coinpayap: JIT profile information will not be recorded: profile file does not exits.
W/yapp.coinpayap: JIT profile information will not be recorded: profile file does not exits.
I/chatty: uid=10128(com.example.coinpayapp.coinpayapp) identical 3 lines
W/yapp.coinpayap: JIT profile information will not be recorded: profile file does not exits.
W/yapp.coinpayap: JIT profile information will not be recorded: profile file does not exits.
    JIT profile information will not be recorded: profile file does not exits.
I/InstantRun: starting instant run server: is main process
W/yapp.coinpayap: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
W/yapp.coinpayap: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
W/System.err: java.io.IOException: Cleartext HTTP traffic to [ip] not permitted
W/System.err:     at com.android.okhttp.HttpHandler$CleartextURLFilter.checkURLPermitted(HttpHandler.java:115)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:458)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:244)
        at com.example.coinpayapp.coinpayapp.BackWork.doInBackground(BackWork.java:70)
        at com.example.coinpayapp.coinpayapp.BackWork.doInBackground(BackWork.java:21)
        at android.os.AsyncTask$2.call(AsyncTask.java:333)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
D/OpenGLRenderer: Skia GL Pipeline
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
I/Adreno: QUALCOMM build                   : 8397a22, Iaa6c380d7d
    Build Date                       : 12/02/18
    OpenGL ES Shader Compiler Version: EV031.25.03.02
    Local Branch                     : 
    Remote Branch                    : 
    Remote Branch                    : 
    Reconstruct Branch               : 
    Build Config                     : S L 6.0.7 AArch64
D/vndksupport: Loading /vendor/lib64/hw/gralloc.msm8996.so from current namespace instead of sphal namespace.
I/Adreno: PFP: 0x005ff087, ME: 0x005ff063
I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 2
D/vndksupport: Loading /vendor/lib64/hw/[email protected] from current namespace instead of sphal namespace.
D/vndksupport: Loading /vendor/lib64/hw/gralloc.msm8996.so from current namespace instead of sphal namespace.
Application terminated.



the line where it errors out is this.

InputStream in = urlconn.getInputStream();



This is under the type server.

What has caused this to start happening? Also when i install it on my phone i get this quick pop up but disappears really quick.

This post has been edited by Bobby_Bubbles: 28 March 2019 - 08:50 AM


Is This A Good Question/Topic? 0
  • +

Replies To: app pulling null from link

#2 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 291
  • Joined: 13-March 18

Re: app pulling null from link

Posted 28 March 2019 - 09:30 AM

so im an idiot and didnt bother googling the problem.

i stuck this in manifest inside application.

android:usesCleartextTraffic="true"



Fixed the problem.

Please close this thread.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1