Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Aybek_Dzhumadillayev_diplomEN.docx
Скачиваний:
20
Добавлен:
25.02.2016
Размер:
630.99 Кб
Скачать

3.2.6 JsonParser.Java[11]

This class is used for connection with application and php scripts. The application gives parameters and this class converts this to comfortable json format and opens http connection. It also sends information to the server and receives from the server.

Description of the functions and variables of the class JSONParser.java

public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) – in JSONParser.java only one method. This method has three parameters: String url is used for getting the address that will send this information, String method is used to know the method of connection Get or Post, List<NameValuePair> params is parameters to send.

Table 7 – variables of class JSONParser.java

Name

Type

description

Is

InputStream

For saving InputStream

jObj

JSONObject

For saving object of JSON

Continue of Table 7 – variables of class JSONParser.java

Json

String

For saving json

method

String

For knowing what kind of method

httpClient

DefaultHttpClient

For saving httpclient

httpPost

HttpPost

For saving httpPost

httpResponse

HttpResponse

For saving HttpResponse

httpClient

DefaultHttpClient

For saving DefaultHttpClient

paramString

String

All parameters

reader

BufferedReader

Reading from stream

Line

String

One line of stream

In Figure - 9 is drawn example class JSONParser.java

public class JSONParser {

static InputStream is = null;

static JSONObject jObj = null;

static String json = "";

public JSONParser() {

}

public JSONObject makeHttpRequest(String url, String method,

List<NameValuePair> params) {

try {

if(method == "POST"){

DefaultHttpClient httpClient = new DefaultHttpClient();

HttpPost httpPost = new HttpPost(url);

httpPost.setEntity(new UrlEncodedFormEntity(params));

HttpResponse httpResponse = httpClient.execute(httpPost);

HttpEntity httpEntity = httpResponse.getEntity();

is = httpEntity.getContent();

}else if(method == "GET"){

DefaultHttpClient httpClient = new DefaultHttpClient();

String paramString = URLEncodedUtils.format(params, "utf-8");

url += "?" + paramString;

HttpGet httpGet = new HttpGet(url);

HttpResponse httpResponse = httpClient.execute(httpGet);

HttpEntity httpEntity = httpResponse.getEntity();

is = httpEntity.getContent();

}

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

try {

BufferedReader reader = new BufferedReader(new InputStreamReader(

is, "iso-8859-1"), 8);

StringBuilder sb = new StringBuilder();

String line = null;

line = reader.readLine();

sb.append(line + "\n");

is.close();

json = sb.toString();

} catch (Exception e) {

Log.e("Buffer Error", "Error converting result " + e.toString());

}

// try parse the string to a JSON object

try {

jObj = new JSONObject(json);

} catch (JSONException e) {

Log.e("JSON Parser", "Error parsing data " + e.toString());

}

// return JSON String

return jObj;

}

}

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]