Txtweb postcode sample code

What is Txtweb ?

Txtweb is a web platform which can be accessed by the sms for free of cost. Here Anyone can deploy any app and which can be accessed worldwide.

Do you want to learn how to program in txtweb ? then Learn and start now.

I have deployed one app named postcode in the txtweb and the below is the source code of the app,

package in.javadomain;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import org.json.JSONException;
import org.json.JSONObject;

public class PostCode {

	public static void main(String[] args) {
		PostCode pc = new PostCode();
		String postCode = pc.getPostCode("arcot");
	}

	public String getPostCode(String placeName) {
		String postCode = null;
		try {
			URL url = new URL("http://www.getpincode.info/api/pincode?q="
					+ placeName + "");
			URLConnection connection = url.openConnection();
			String line;
			StringBuilder builder = new StringBuilder();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					connection.getInputStream()));
			while ((line = reader.readLine()) != null) {
				builder.append(line);
			}

			JSONObject json = new JSONObject(builder.toString());
			System.out.println("PostCode of " + placeName + " is  "
					+ json.get("pincode"));
		} catch (IOException e) {
			postCode = "Server May be Busy, Try Again Later!";
		} catch (JSONException e) {
			postCode = "Server May be Busy, Try Again Later!";
		}
		return placeName + "<br>" + "App By Javadomain.in";
	}
}

Output:

PostCode of arcot is  632503

Thanks for reading this article. If you found anything wrong then kindly let me know.

281 total views, 1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *