How To Do This In Java

Hi Guys How To Do This In Java :-1: :-1: :-1:
blocks

What have you tried?

I Tried It I Is Not Working

And I Need Help With Posting In Web In JAVA

I Tried It But I Don’t Know How Read The Response

Good luck with your journey. :+1:

2 Likes

can you help me out with this

For GET request:

For POST request:

@Xoma Started Compiling Project WebPost
Buildfile: /compiler/android/build.xml

javac:
[mkdir] Created dir: /compiler/android/build/dPXpo/classes
[javac] Compiling 1 source file to /compiler/android/build/dPXpo/classes
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] Note: Wrote file file:/compiler/android/build/dPXpo/classes/simple_components.json
[javac] Note: Wrote file file:/compiler/android/build/dPXpo/classes/simple_components.txt
[javac] Note: Wrote file file:/compiler/android/build/dPXpo/classes/simple_components_build_info.json
[javac] Note: Wrote file file:/compiler/android/build/dPXpo/classes/AutogeneratedOdeMessages.java
[javac] Note: Wrote file file:/compiler/android/build/dPXpo/classes/ComponentsTranslation.java
[javac] /compiler/android/src/dPXpo/com/web/post/WebPost.java:46: error: unreported exception MalformedURLException; must be caught or declared to be thrown
[javac] URL obj = new URL(“https://www.google.com”);
[javac] ^
[javac] /compiler/android/src/dPXpo/com/web/post/WebPost.java:47: error: unreported exception IOException; must be caught or declared to be thrown
[javac] HttpURLConnection httpConnection = (HttpURLConnection)obj.openConnection();
[javac] ^
[javac] /compiler/android/src/dPXpo/com/web/post/WebPost.java:49: error: unreported exception ProtocolException; must be caught or declared to be thrown
[javac] httpConnection.setRequestMethod(“POST”); //request method
[javac] ^
[javac] /compiler/android/src/dPXpo/com/web/post/WebPost.java:51: error: unreported exception IOException; must be caught or declared to be thrown
[javac] if (httpConnection.getResponseCode() == 200) {
[javac] ^
[javac] /compiler/android/src/dPXpo/com/web/post/WebPost.java:52: error: unreported exception IOException; must be caught or declared to be thrown
[javac] bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
[javac] ^
[javac] /compiler/android/src/dPXpo/com/web/post/WebPost.java:58: error: unreported exception IOException; must be caught or declared to be thrown
[javac] while ((line = bufferedReader.readLine()) != null){
[javac] ^
[javac] /compiler/android/src/dPXpo/com/web/post/WebPost.java:61: error: unreported exception IOException; must be caught or declared to be thrown
[javac] bufferedReader.close();
[javac] ^
[javac] 7 errors

Use try-catch or add an IO Exception.

@SimpleFunction(description = "b")
    public void TestFunction(){ try
          URL obj = new URL("https://www.google.com");
      HttpURLConnection httpConnection = (HttpURLConnection)obj.openConnection();
      httpConnection.setDoOutput(true); //if you want output
      httpConnection.setRequestMethod("POST"); //request method
      BufferedReader bufferedReader = null;
      if (httpConnection.getResponseCode() == 200) {
        bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
      } else {
        bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getErrorStream()));
      } 
      StringBuilder content = new StringBuilder();
      String line;
      while ((line = bufferedReader.readLine()) != null){
        content.append(line).append("\n"); 
      }
      bufferedReader.close();
      final String con = String.valueOf(content); //post wherever you need

Imports


import android.app.Activity;
import android.content.Context;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

Error

Started Compiling Project WebPost
Buildfile: /compiler/android/build.xml

javac:
[mkdir] Created dir: /compiler/android/build/oQpjH/classes
[javac] Compiling 1 source file to /compiler/android/build/oQpjH/classes
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] /compiler/android/src/oQpjH/com/web/post/WebPost.java:45: error: ‘{’ expected
[javac] public void TestFunction(){ try
[javac] ^
[javac] /compiler/android/src/oQpjH/com/web/post/WebPost.java:45: error: ‘try’ without ‘catch’, ‘finally’ or resource declarations
[javac] public void TestFunction(){ try
[javac] ^
[javac] /compiler/android/src/oQpjH/com/web/post/WebPost.java:65: error: reached end of file while parsing
[javac] }
[javac] ^
[javac] 3 errors
[javac] 1 warning

full code

package com.web.post;

import android.app.Activity;

import android.content.Context;

import com.google.appinventor.components.annotations.*;

import com.google.appinventor.components.common.ComponentCategory;

import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;

import com.google.appinventor.components.runtime.ComponentContainer;

import com.google.appinventor.components.runtime.EventDispatcher;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.net.HttpURLConnection;

import java.net.URL;

@DesignerComponent(

        version = 1,

        description = "",

        category = ComponentCategory.EXTENSION,

        nonVisible = true,

        iconName = "")

@SimpleObject(external = true)

//Libraries

@UsesLibraries(libraries = "")

//Permissions

@UsesPermissions(permissionNames = "")

public class WebPost extends AndroidNonvisibleComponent {

    //Activity and Context

    private Context context;

    private Activity activity;

    public WebPost(ComponentContainer container){

        super(container.$form());

        this.activity = container.$context();

        this.context = container.$context();

    }

    @SimpleFunction(description = "b")

    public void TestFunction(){ try-catch

          URL obj = new URL("https://www.google.com");

      HttpURLConnection httpConnection = (HttpURLConnection)obj.openConnection();

      httpConnection.setDoOutput(true); //if you want output

      httpConnection.setRequestMethod("POST"); //request method

      BufferedReader bufferedReader = null;

      if (httpConnection.getResponseCode() == 200) {

        bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));

      } else {

        bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getErrorStream()));

      } 

      StringBuilder content = new StringBuilder();

      String line;

      while ((line = bufferedReader.readLine()) != null){

        content.append(line).append("\n"); 

      }

      bufferedReader.close();

      final String con = String.valueOf(content); //post wherever you need

    }

}

@Xoma what should i do now

This is not the right method

try{}
catch{}

Developers need to use it to catch Any error

Like This

try{your code to run}
catch(Exception e){
e.printStackTrace();
}

try is where your code run in.
catch is what that catch errors in your code wrote in try block.

Error

Started Compiling Project WebPost
Buildfile: /compiler/android/build.xml

javac:
[mkdir] Created dir: /compiler/android/build/NDVGR/classes
[javac] Compiling 1 source file to /compiler/android/build/NDVGR/classes
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] /compiler/android/src/NDVGR/com/web/post/WebPost.java:45: error: ';' expected
[javac] public void TestFunction()try{
[javac] ^
[javac] /compiler/android/src/NDVGR/com/web/post/WebPost.java:65: error: illegal start of type
[javac] catch(Exception e){
[javac] ^
[javac] 2 errors
[javac] 1 warning

Code

import android.app.Activity;
import android.content.Context;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;


@DesignerComponent(
        version = 1,
        description = "",
        category = ComponentCategory.EXTENSION,
        nonVisible = true,
        iconName = "")

@SimpleObject(external = true)
//Libraries
@UsesLibraries(libraries = "")
//Permissions
@UsesPermissions(permissionNames = "")

public class WebPost extends AndroidNonvisibleComponent {

    //Activity and Context
    private Context context;
    private Activity activity;

    public WebPost(ComponentContainer container){
        super(container.$form());
        this.activity = container.$context();
        this.context = container.$context();
    }

    @SimpleFunction(description = "b")
    public void TestFunction()try{
          URL obj = new URL("https://www.google.com");
      HttpURLConnection httpConnection = (HttpURLConnection)obj.openConnection();
      httpConnection.setDoOutput(true); //if you want output
      httpConnection.setRequestMethod("POST"); //request method
      BufferedReader bufferedReader = null;
      if (httpConnection.getResponseCode() == 200) {
        bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
      } else {
        bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getErrorStream()));
      } 
      StringBuilder content = new StringBuilder();
      String line;
      while ((line = bufferedReader.readLine()) != null){
        content.append(line).append("\n"); 
      }
      bufferedReader.close();
      final String con = String.valueOf(content); //post wherever you need
      
    }
    catch(Exception e){
    e.printStackTrace();
    }
}

@Raja_HarshVardhanSolanki i got this error

You missed a braces here. It should be public void TestFunction(){.
Also try and catch is not enough for that. You need to use the thread for it. A example is here for sending a get request to URL.

try function only run in a method.

try this:

public void TestFunction(){
try{***}

catch{***}
}

how to show the data

Data(httpConnection.getInputStream());

full code

import android.app.Activity;

import android.content.Context;

import com.google.appinventor.components.annotations.*;

import com.google.appinventor.components.common.ComponentCategory;

import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;

import com.google.appinventor.components.runtime.ComponentContainer;

import com.google.appinventor.components.runtime.EventDispatcher;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.net.HttpURLConnection;

import java.net.URL;

@DesignerComponent(

        version = 1,

        description = "",

        category = ComponentCategory.EXTENSION,

        nonVisible = true,

        iconName = "")

@SimpleObject(external = true)

//Libraries

@UsesLibraries(libraries = "")

//Permissions

@UsesPermissions(permissionNames = "")

public class WebPost extends AndroidNonvisibleComponent {

    //Activity and Context

    private Context context;

    private Activity activity;

    public WebPost(ComponentContainer container){

        super(container.$form());

        this.activity = container.$context();

        this.context = container.$context();

    }

    @SimpleFunction(description = "b")

   public void run() {

            try {

          URL obj = new URL("https://www.google.com");

      HttpURLConnection httpConnection = (HttpURLConnection)obj.openConnection();

      httpConnection.setDoOutput(true); //if you want output

      httpConnection.setRequestMethod("POST"); //request method

      BufferedReader bufferedReader = null;

      if (httpConnection.getResponseCode() == 200) {

        bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));

        Data(httpConnection.getInputStream());

      } else {

        bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getErrorStream()));

      } 

      StringBuilder content = new StringBuilder();

      String line;

      while ((line = bufferedReader.readLine()) != null){

        content.append(line).append("\n"); 

      }

      bufferedReader.close();

      final String con = String.valueOf(content); //post wherever you need

      

    }

    catch(Exception e){

    e.printStackTrace();

    }

   }

}

This is the error

Started Compiling Project WebPost
Buildfile: /compiler/androidX/build.xml

javac:
[mkdir] Created dir: /compiler/androidX/build/Qlqhp/classes
[javac] Compiling 1 source file to /compiler/androidX/build/Qlqhp/classes
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] Note: Wrote file file:/compiler/androidX/build/Qlqhp/classes/simple_components.json
[javac] Note: Wrote file file:/compiler/androidX/build/Qlqhp/classes/simple_components.txt
[javac] Note: Wrote file file:/compiler/androidX/build/Qlqhp/classes/simple_components_build_info.json
[javac] Note: Wrote file file:/compiler/androidX/build/Qlqhp/classes/AutogeneratedOdeMessages.java
[javac] Note: Wrote file file:/compiler/androidX/build/Qlqhp/classes/ComponentsTranslation.java
[javac] /compiler/androidX/src/Qlqhp/com/web/post/WebPost.java:54: error: cannot find symbol
[javac] Data(httpConnection.getInputStream());
[javac] ^
[javac] symbol: method Data(InputStream)
[javac] location: class WebPost
[javac] 1 error

@Raja_HarshVardhanSolanki @iamwsumit ???
what is the mistake

Have you made Data event in your code? If not then you need to make it.
One more thing you should to do to get the data
Use

Data(bufferedReader.readLine()) ;

Instead of passing input stream to the param.

From where you call this Data function.