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

3.2.5 Call.Java

This class uses when user selects Call without backup. In this page user can see all his contacts and his phone number. When presses to one of the contacts application will automatically call to this contact.

protected void onCreate (Bundle savedInstanceState) - it is a function of the main interface for querying

public void onClick (View v) - this function starts when you press the button Button, and if the entered information is correct, sends the information to the database and logs the user

public void onPreExecute () - this function creates a PDialog and shows until the program receives data from the Base

protected String doInBackground (String. .. args) - main function is to register

protected void onPostExecute (String file_url) - sends data to the interface

Table 6 – variables of class Call.java

Name

Type

description

jsonParser

JSONParser

To send information to the base

Submit

Button

If the information entered is correct, sends the information to the database and logs the user

Phone

String

phone number contact

name

String

phone name of contact

Reg

Button

Button registration

Login

Button

Button for login

List

ListView

List of all contacts

Pho

TextView

List of phones

url

String

URL php script

Success

Int

For validation

PDialog

ProgressDialog

Animation boot

Login

String

User login

Ok

Boolean

To verify the correct password

Ko

Boolean

To check the stored contacts

3.2.6 JSONParser.java

This class is used for connection with application and php scripts. Application gives parameters and this class converts this to comfortable json format and opens http connection. And sends information to server and receives from 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 parametres: String url is used for getting for what address will send this information, String method is used to know method of connection Get or Post, List<NameValuePair> params is parameters to send.

Table 6 – variables of class JSONParser.java

Name

Type

description

Is

InputStream

For saving InputStream

jObj

JSONObject

For saving object of JSON

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 you can see the class JSONParser.java for example of one class

public class JSONParser {

static InputStream is = null;

static JSONObject jObj = null;

static String json = "";

// constructor

public JSONParser() {

}

// function get json from url

// by making HTTP POST or GET mehtod

public JSONObject makeHttpRequest(String url, String method,

List<NameValuePair> params) {

// Making HTTP request

try {

// check for request method

if(method == "POST"){

// request method is POST

// defaultHttpClient

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"){

// request method is 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;

}

}

Figure 9 - JSONParser.java

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