Wednesday, 26 July 2017
Sunday, 16 July 2017
Error Android : android.os.NetworkOnMainThreadException?
Sometimes when you try doing a background progress or performing network request. Android will give this error. So what you need to do is passing the execution to AsyncTask class to seperate it from the main thread. This can refer as example down below :
private class sendFile extends AsyncTask<String, Void, String> { private Exception exception; @Override protected String doInBackground(String... urls) { try { OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"); RequestBody body = RequestBody.create(mediaType, "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"apikey\"\r\n\r\n4clawHkacJICpzNv_1lTC8IGGBxyVADY4G3rpeMRWKg\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"); Request requestQueue = new Request.Builder() .url("https://api.webempath.net/v2/analyzeWav") .post(body) .addHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW") .addHeader("cache-control", "no-cache") .addHeader("postman-token", "1ac1340c-beb9-162d-f583-e2bd77b7355a") .build(); try { Response response = client.newCall(requestQueue).execute(); String mResponse = response.body().string(); Log.i("Response",mResponse); } catch (IOException e) { e.printStackTrace(); } }catch (Exception e) { this.exception = e; return null; } return null; } @Override protected void onPostExecute(String result) { responseText.setText(result); } }
Volley POST Request Body
This happens whenever web services requesting data through json body . Below is the code example of using json volley request with body :
/*Some example that you can pass your data through request body*/RequestQueue requestQueue = Volley.newRequestQueue(this); JSONObject jsonBodyObj = new JSONObject(); String url = "https://api.webempath.net/v2/analyzeWav"; try{ jsonBodyObj.put("key1", "value"); jsonBodyObj.put("key2", "value"); }catch (JSONException e){ e.printStackTrace(); } final String requestBody = jsonBodyObj.toString(); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>(){ @Override public void onResponse(JSONObject response) { Log.i("Response",String.valueOf(response)); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { VolleyLog.e("Error: ", error.getMessage()); } }){ @Override public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/json"); return headers; } @Override public byte[] getBody() { try { return requestBody == null ? null : requestBody.getBytes("utf-8"); } catch (UnsupportedEncodingException uee) { VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8"); return null; } } }; requestQueue.add(jsonObjectRequest);
Monday, 10 July 2017
DNS Flush
What basically it do ?
The domain name service (DNS) act as converter that change domain name into an IP address. To check lists of DNS mapping , you can write down ipconfig/displaydns. Here we can see the regular website of IP Address that we browsing like htpps://www.google.com. When you repeatedly use a same URL, our computer will lookup IP Address inside local cache instead fetching from DNS, this is because IP address are not regularly changed. Heres the thing, when the server were moved to another website or perhaps IP Address has been changed this probably will cause you to see old website for a while. When you do a ipconfig /flushdns, your system clears the cache of name to ip entries and reloads them from the connected DNS server.
Figure 1.0 - DNS entry
Figure 2.0 - flushing DNS entries
Figure 1.0 - DNS entry
Figure 2.0 - flushing DNS entries
Monday, 3 July 2017
Topic 1 ( Ionic ) - Onclick Button
Onclick function
Passing parameter "string" from users.html to user.html communicate through .ts file using onclick function.
users.html
<button ion-button (click)="onLoadUser('Syafiq')">User 'Syafiq'</button>
users.ts
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {UserPage} from "./user/user";
@IonicPage()
@Component({
selector: 'page-users',
templateUrl: 'users.html',
})
export class Users {
constructor(public navCtrl: NavController) {
}
onLoadUser(name:string){
this.navCtrl.push(UserPage,{userName : name},{animate: true, direction: 'forward'});
}
}
user.html
<ion-header>
<ion-navbar>
<ion-title>{{ name }}</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<p>Hi, I'm {{ name }}</p>
<ion-content>
user.ts
import { Component, OnInit } from "@angular/core";
import { NavParams } from "ionic-angular";
@Component({
selector: 'page-user',
templateUrl : 'user.html'
})
export class UserPage implements OnInit {
name : string;
constructor (private navParams: NavParams){
}
ngOnInit(){
this.name = this.navParams.get('userName');
}
}
Source:
Maximilian Udemy Instructor